home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Modified TE32K 1.2 / TE32K.c next >
Encoding:
C/C++ Source or Header  |  1994-01-10  |  81.5 KB  |  3,290 lines  |  [TEXT/KAHL]

  1. /*    94/01/02 aih checks for memory availability */
  2.  
  3. /* I've added the following lines */
  4.     #include "MemoryLib.h"
  5.     #define HiliteMode            (*(char *) (0x938))
  6.     #define NewHandle(n)        HandleBegin(n)
  7.     #define DisposHandle(h)        HandleEnd(h)
  8.     #define GetHandleSize(h)    HandleSize(h)
  9.     #define SetHandleSize(h, n)    HandleSizeSet((h), (n))
  10. /* end of lines I've added */
  11.  
  12. #include    "TE32K.h"
  13.  
  14. #define    EXTRALINESTARTS        32L
  15. #define    EXTRATEXTBUFF        256L
  16.  
  17. #define    LEFTARROW            28
  18. #define    RIGHTARROW            29
  19. #define UPARROW                30
  20. #define DOWNARROW            31
  21. #define    TAB                    '\t'
  22. #define DELETE                0x08
  23. #define    RETURN                0x0D
  24. #define    ENTER                0x03
  25.  
  26.  
  27.  
  28. Handle            TE32KScrpHandle = nil;
  29. long            TE32KScrpLength = 0L;
  30. TE32KHandle        clickedTE32KH = nil;
  31.  
  32.  
  33.  
  34. static long        LineEndIndex(long,TE32KHandle);
  35. static void        CalParagraph(long,TE32KHandle,long *,long *);
  36. static long     paraLines(long,TE32KHandle);
  37. static void     updateLine(long,TE32KHandle,int,LongRect *);
  38. static void        invertSelRange(long,long,TE32KHandle);
  39. static void        xorCaret(TE32KHandle);
  40. static long        indexToLine(long,TE32KHandle);
  41. static int        shiftKeyDown(void);
  42. static void     MyClicker(void);
  43. static void     MyClickLoop(void);
  44.  
  45.  
  46.  
  47.  
  48. void    TE32KSetFontStuff(int txFont,int txFace,int txMode,int txSize,TE32KHandle theTE32KHandle)
  49. {
  50. register int        i;
  51. int                    oldFont,oldFace,oldSize,oldMode;
  52. GrafPtr                oldPort;
  53. FontInfo            theFontInfo;
  54.  
  55.  
  56.     (**theTE32KHandle).txFont = txFont;
  57.     (**theTE32KHandle).txFace = txFace;
  58.     (**theTE32KHandle).txMode = txMode;
  59.     (**theTE32KHandle).txSize = txSize;
  60.     
  61.     GetPort(&oldPort);
  62.     SetPort((**theTE32KHandle).inPort);
  63.     oldFont = ((**theTE32KHandle).inPort)->txFont;
  64.     oldFace = ((**theTE32KHandle).inPort)->txFace;
  65.     oldSize = ((**theTE32KHandle).inPort)->txSize;
  66.     oldMode = ((**theTE32KHandle).inPort)->txMode;
  67.     
  68.     TextFont((**theTE32KHandle).txFont);
  69.     TextFace((**theTE32KHandle).txFace);
  70.     TextSize((**theTE32KHandle).txSize);
  71.     TextMode((**theTE32KHandle).txMode);
  72.     
  73.     
  74.     for (i=0;i<256;i++)
  75.         (**theTE32KHandle).theCharWidths[i] = CharWidth((unsigned char) i);
  76.     
  77.     GetFontInfo(&theFontInfo);
  78.     
  79.     (**theTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  80.     (**theTE32KHandle).fontAscent = theFontInfo.ascent;
  81.     
  82.     if ((**theTE32KHandle).tabChars)
  83.         (**theTE32KHandle).tabWidth = (**theTE32KHandle).tabChars * (**theTE32KHandle).theCharWidths[' '];
  84.     
  85.     TextFont(oldFont);
  86.     TextFace(oldFace);
  87.     TextSize(oldSize);
  88.     TextMode(oldMode);
  89.     
  90.     SetPort(oldPort);
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. void    SetLongRect(LongRect *theLongRect,long left,long top,long right,long bottom)
  98. {
  99.     theLongRect->left = left;
  100.     theLongRect->top = top;
  101.     theLongRect->right = right;
  102.     theLongRect->bottom = bottom;
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. void    RectToLongRect(Rect *theRect,LongRect *theLongRect)
  110. {
  111.     theLongRect->left = (long) theRect->left;
  112.     theLongRect->top = (long) theRect->top;
  113.     theLongRect->right = (long) theRect->right;
  114.     theLongRect->bottom = (long) theRect->bottom;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. void    LongRectToRect(LongRect *theLongRect,Rect *theRect)
  121. {
  122.     if (theLongRect->left < -32768L)
  123.         theRect->left = (int) -32768;
  124.     else if (theLongRect->left > 32767L)
  125.         theRect->left = (int) 32767;
  126.     else
  127.         theRect->left = (int) theLongRect->left;
  128.     
  129.     if (theLongRect->top < -32768L)
  130.         theRect->top = (int) -32768;
  131.     else if (theLongRect->top > 32767L)
  132.         theRect->top = (int) 32767;
  133.     else
  134.         theRect->top = (int) theLongRect->top;
  135.     
  136.     if (theLongRect->right < -32768L)
  137.         theRect->right = (int) -32768;
  138.     else if (theLongRect->right > 32767L)
  139.         theRect->right = (int) 32767;
  140.     else
  141.         theRect->right = (int) theLongRect->right;
  142.     
  143.     if (theLongRect->bottom < -32768L)
  144.         theRect->bottom = (int) -32768;
  145.     else if (theLongRect->bottom > 32767L)
  146.         theRect->bottom = (int) 32767;
  147.     else
  148.         theRect->bottom = (int) theLongRect->bottom;
  149. }
  150.  
  151.  
  152.  
  153. void    OffsetLongRect(LongRect *theLongRect, long x, long y)
  154. {
  155.         theLongRect->left += x;
  156.         theLongRect->top += y;
  157.         theLongRect->right += x;
  158.         theLongRect->bottom += y;
  159. }
  160.  
  161.  
  162.  
  163.  
  164. static long    indexToLine(long selIndex,TE32KHandle theTE32KHandle)
  165. {
  166. register long    i,delta;
  167.  
  168.     if (theTE32KHandle)
  169.     {
  170.         if (selIndex<=0L || (**theTE32KHandle).nLines<=1L || (**theTE32KHandle).teLength<1L)
  171.             return(0L);
  172.         
  173.         else if (selIndex >= (**theTE32KHandle).teLength)
  174.             return((long) ((**theTE32KHandle).nLines - 1L));
  175.         
  176.         else
  177.         {
  178.             i = ((**theTE32KHandle).nLines) >> 1;
  179.             
  180.             delta = ((**theTE32KHandle).nLines) >> 1;
  181.             if (delta < 1L)
  182.                 delta = 1L;
  183.             
  184.             while (delta > 0L)
  185.             {
  186.                 if (selIndex == (**theTE32KHandle).lineStarts[i])
  187.                     delta = 0L;
  188.                 
  189.                 else if (selIndex > (**theTE32KHandle).lineStarts[i])
  190.                 {
  191.                     if (selIndex < (**theTE32KHandle).lineStarts[i+1])
  192.                         delta = 0L;
  193.                     
  194.                     else
  195.                         i += delta;
  196.                 }
  197.                 
  198.                 else
  199.                     i -= delta;
  200.                 
  201.                 if (delta)
  202.                 {
  203.                     delta >>= 1;
  204.                     
  205.                     if (delta < 1L)
  206.                         delta = 1L;
  207.                 }
  208.             }
  209.         }
  210.         
  211.         if (i < 0L)
  212.             i = 0L;
  213.         else if (i >= (**theTE32KHandle).nLines)
  214.             i = (**theTE32KHandle).nLines - 1L;
  215.         
  216.         return((long) i);
  217.     }
  218.     
  219.     else
  220.         return((long) 0L);
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. static void    xorCaret(TE32KHandle theTE32KHandle)
  230. {
  231. GrafPtr            oldPort;
  232. PenState        oldPenState;
  233. Point            selPt;
  234. RgnHandle        oldClipRgn;
  235. Rect            theClipRect;
  236.  
  237.     if (theTE32KHandle && (**theTE32KHandle).active && (**theTE32KHandle).selStart==(**theTE32KHandle).selEnd)
  238.     {
  239.         if (!(**theTE32KHandle).caretState && ((**theTE32KHandle).selStart < 0 || (**theTE32KHandle).selEnd > (**theTE32KHandle).teLength))
  240.             return;
  241.         
  242.         GetPort(&oldPort);
  243.         SetPort((**theTE32KHandle).inPort);
  244.         
  245.         GetPenState(&oldPenState);
  246.         oldClipRgn = NewRgn();
  247.         GetClip(oldClipRgn);
  248.         
  249.         theClipRect.left = (int) ((**theTE32KHandle).viewRect.left);
  250.         theClipRect.top = (int) ((**theTE32KHandle).viewRect.top);
  251.         theClipRect.right = (int) ((**theTE32KHandle).viewRect.right);
  252.         theClipRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  253.         
  254.         ClipRect(&theClipRect);
  255.         
  256.         PenNormal();
  257.         
  258.         PenMode(patXor);
  259.         
  260.         if ((**theTE32KHandle).selPoint.h < -32768L)
  261.             selPt.h = (int) -32768;
  262.         else if ((**theTE32KHandle).selPoint.h > 32767L)
  263.             selPt.h = (int) 32767;
  264.         else
  265.             selPt.h = (int) (**theTE32KHandle).selPoint.h;
  266.         
  267.         if ((**theTE32KHandle).selPoint.v < -32768L)
  268.             selPt.v = (int) -32768;
  269.         else if ((**theTE32KHandle).selPoint.v > 32767L)
  270.             selPt.v = (int) 32767;
  271.         else
  272.             selPt.v = (int) (**theTE32KHandle).selPoint.v;
  273.         
  274.         MoveTo(selPt.h - 1,selPt.v);
  275.         Line(0,-(**theTE32KHandle).fontAscent);
  276.         
  277.         (**theTE32KHandle).caretTime = TickCount() + GetCaretTime();
  278.         (**theTE32KHandle).caretState = !(**theTE32KHandle).caretState;
  279.         
  280.         SetClip(oldClipRgn);
  281.         DisposeRgn(oldClipRgn);
  282.         
  283.         SetPenState(&oldPenState);
  284.         SetPort(oldPort);
  285.     }
  286. }
  287.  
  288.  
  289.  
  290.  
  291. void    TE32KInit()
  292. {
  293.     TE32KScrpHandle = NewHandle(0L);
  294.     TE32KScrpLength = 0L;
  295. }
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302. TE32KHandle        TE32KNew(LongRect *destRect,LongRect *viewRect)
  303. {
  304. TE32KHandle        newTE32KHandle;
  305. Handle            hText;
  306. GrafPtr            activePort;
  307. FontInfo        theFontInfo;
  308. LongPoint        selPt;
  309.  
  310.     newTE32KHandle = (TE32KHandle) NewHandle((long) sizeof(TE32KRec) + (long) sizeof(long)*EXTRALINESTARTS);
  311.     if (MemError() || StripAddress(newTE32KHandle)==nil)
  312.         return((TE32KHandle) nil);
  313.     
  314.     hText = NewHandle(EXTRATEXTBUFF);
  315.     if (MemError() || StripAddress(hText)==nil)
  316.     {
  317.         DisposHandle((Handle) newTE32KHandle);
  318.         return((TE32KHandle) nil);
  319.     }
  320.     
  321.     (**newTE32KHandle).destRect = *destRect;
  322.     (**newTE32KHandle).viewRect = *viewRect;
  323.     
  324.     GetPort(&activePort);
  325.     GetFontInfo(&theFontInfo);
  326.     
  327.     (**newTE32KHandle).lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
  328.     (**newTE32KHandle).fontAscent = theFontInfo.ascent;
  329.     
  330.     (**newTE32KHandle).selStart = 0L;
  331.     (**newTE32KHandle).selEnd = 0L;
  332.     
  333.     (**newTE32KHandle).teLength = 0L;
  334.     (**newTE32KHandle).hText = hText;
  335.     
  336.     (**newTE32KHandle).txFont = activePort->txFont;
  337.     (**newTE32KHandle).txFace = activePort->txFace;
  338.     (**newTE32KHandle).txMode = activePort->txMode;
  339.     (**newTE32KHandle).txSize = activePort->txSize;
  340.     
  341.     (**newTE32KHandle).inPort = activePort;
  342.     
  343.     (**newTE32KHandle).tabWidth = 25;
  344.     (**newTE32KHandle).tabChars = 0;
  345.     
  346.     (**newTE32KHandle).maxLineWidth = 32767;
  347.     
  348.     (**newTE32KHandle).clikStuff = FALSE;
  349.     
  350.     (**newTE32KHandle).crOnly = 0x00;
  351.     
  352.     (**newTE32KHandle).nLines = 1L;
  353.     (**newTE32KHandle).lineStarts[0] = 0L;
  354.     (**newTE32KHandle).lineStarts[1] = 0L;
  355.     
  356.     (**newTE32KHandle).active = TRUE;
  357.     (**newTE32KHandle).caretState = FALSE;
  358.     (**newTE32KHandle).caretTime = TickCount();
  359.     
  360.     (**newTE32KHandle).clickTime = TickCount();
  361.     (**newTE32KHandle).clickLoc = -1L;
  362.     
  363.     TE32KGetPoint((**newTE32KHandle).selStart,&selPt,newTE32KHandle);
  364.     
  365.     (**newTE32KHandle).selPoint = selPt;
  366.     
  367.     (**newTE32KHandle).clikLoop = nil;
  368.     
  369.     TE32KSetFontStuff((**newTE32KHandle).txFont,(**newTE32KHandle).txFace,(**newTE32KHandle).txMode,(**newTE32KHandle).txSize,newTE32KHandle);
  370.     
  371.     return((TE32KHandle) newTE32KHandle);
  372. }
  373.  
  374.  
  375.  
  376.  
  377.  
  378. void TE32KDispose(TE32KHandle theTE32KHandle)
  379. {
  380.     if (theTE32KHandle)
  381.     {
  382.         if ((**theTE32KHandle).hText)
  383.             DisposHandle((**theTE32KHandle).hText);
  384.         
  385.         DisposHandle(theTE32KHandle);
  386.     }
  387. }
  388.  
  389.  
  390.  
  391.  
  392. void    TE32KCalText(TE32KHandle theTE32KHandle)
  393. {
  394. register unsigned char    *charPtr;
  395. register long            charCount;
  396. register int            *theCharWidths,lineLength,crOnly,maxLineWidth;
  397. register unsigned char    ch;
  398. long                    nLines,maxLineStarts,sizeTE32KHandle;
  399. unsigned char            *charBase;
  400. Point                    cursorPt;
  401. int                        rightSide,destLeftSide,tabWidth,maxRewind;
  402. unsigned char            *oldCharPtr;
  403. long                    oldCharCount,tempOffset;
  404.  
  405.     if (theTE32KHandle)
  406.     {
  407.         (**theTE32KHandle).lineStarts[0] = 0L;        /* assume the worst can happen and prepare for it */
  408.         (**theTE32KHandle).lineStarts[1] = 0L;
  409.         (**theTE32KHandle).nLines = 1L;
  410.         
  411.         sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  412.         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  413.         
  414.         crOnly = (**theTE32KHandle).crOnly;
  415.         maxLineWidth = (**theTE32KHandle).maxLineWidth;
  416.         
  417.         lineLength = 0L;
  418.         nLines = 0L;
  419.         
  420.         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  421.         charPtr = charBase;
  422.         charCount = (**theTE32KHandle).teLength;
  423.         
  424.         
  425.         if (charCount > 0L)
  426.         {
  427.             rightSide = (int) ((**theTE32KHandle).destRect.right);
  428.             destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  429.             cursorPt.h = destLeftSide;
  430.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  431.             
  432.             theCharWidths = (**theTE32KHandle).theCharWidths;
  433.             
  434.             while (charCount--)
  435.             {
  436.                 ch = *charPtr++;
  437.                 lineLength++;
  438.                 
  439.                 if (!crOnly)
  440.                 {
  441.                     if (ch == TAB)
  442.                         cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  443.                     else if (ch != '\r' && ch != '\n')
  444.                         cursorPt.h += theCharWidths[ch];
  445.                 }
  446.                 
  447.                 if ((ch == '\r' || ch == '\n') || (ch != ' ' && cursorPt.h >= rightSide) || (!crOnly && lineLength > maxLineWidth))
  448.                 {
  449.                     if ((ch != ' ' && cursorPt.h >= rightSide) || (!crOnly && lineLength > maxLineWidth))
  450.                     {
  451.                         /* I should probably add a hook for custom word-breaking */
  452.                         
  453.                         maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[nLines];
  454.                         oldCharPtr = charPtr;
  455.                         oldCharCount = charCount;
  456.                         
  457.                         charPtr--;
  458.                         charCount++;
  459.                         maxRewind--;
  460.                         
  461.                         while (*charPtr != ' ' && maxRewind > 0)
  462.                         {
  463.                             charPtr--;
  464.                             charCount++;
  465.                             maxRewind--;
  466.                         }
  467.                         
  468.                         if (maxRewind <= 0)
  469.                         {
  470.                             charPtr = oldCharPtr;
  471.                             charCount = oldCharCount;
  472.                         }
  473.                         
  474.                         else
  475.                         {
  476.                             charPtr++;
  477.                             charCount--;
  478.                         }
  479.                     }
  480.                     
  481.                     if (nLines >= maxLineStarts)
  482.                     {
  483.                         tempOffset = charPtr - charBase;
  484.                                                 
  485.                         sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  486.                         maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  487.                         
  488.                         SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  489.                         
  490.                         if (MemError())
  491.                             return;
  492.                         
  493.                         charBase = (unsigned char *) *((**theTE32KHandle).hText);
  494.                         charPtr = charBase + tempOffset;
  495.                         theCharWidths = (**theTE32KHandle).theCharWidths;
  496.                     }
  497.                     
  498.                     (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  499.                     
  500.                     cursorPt.h = destLeftSide;
  501.                     lineLength = 0L;
  502.                 }
  503.             }
  504.             
  505.             if (nLines >= maxLineStarts)
  506.             {
  507.                 sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*(nLines + EXTRALINESTARTS);
  508.                 
  509.                 SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  510.                 
  511.                 if (MemError())
  512.                     return;
  513.             }
  514.             
  515.             (**theTE32KHandle).lineStarts[++nLines] = charPtr - charBase;
  516.             
  517.             (**theTE32KHandle).nLines = nLines;
  518.         }
  519.     }
  520. }
  521.  
  522.  
  523.  
  524.  
  525.  
  526. void    TE32KUseTextHandle(Handle hText,TE32KHandle theTE32KHandle)
  527. {
  528. LongPoint    selPt;
  529. long int textLength;
  530.  
  531.     if (theTE32KHandle)
  532.     {
  533.         textLength = GetHandleSize(hText);
  534.  
  535.         if ((**theTE32KHandle).hText)
  536.             DisposHandle((**theTE32KHandle).hText);
  537.                 
  538.         (**theTE32KHandle).hText = hText;
  539.         (**theTE32KHandle).teLength = textLength;
  540.         
  541.         (**theTE32KHandle).selStart = textLength;
  542.         (**theTE32KHandle).selEnd = textLength;
  543.         
  544.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  545.         (**theTE32KHandle).selPoint = selPt;
  546.         
  547.         TE32KCalText(theTE32KHandle);
  548.     }
  549. }
  550.  
  551.  
  552.  
  553.  
  554.  
  555. void    TE32KSetText(Ptr textPtr,long textLength,TE32KHandle theTE32KHandle)
  556. {
  557. Handle        hText;
  558. LongPoint    selPt;
  559.  
  560.     if (theTE32KHandle)
  561.     {
  562.         hText = NewHandle(textLength + EXTRATEXTBUFF);
  563.         
  564.         if (MemError() || StripAddress(hText)==nil)
  565.             return;
  566.         
  567.         if ((**theTE32KHandle).hText)
  568.             DisposHandle((**theTE32KHandle).hText);
  569.         
  570.         HLock(hText);
  571.         BlockMove(textPtr,*hText,textLength);
  572.         HUnlock(hText);
  573.         
  574.         (**theTE32KHandle).hText = hText;
  575.         (**theTE32KHandle).teLength = textLength;
  576.         
  577.         (**theTE32KHandle).selStart = textLength;
  578.         (**theTE32KHandle).selEnd = textLength;
  579.         
  580.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  581.         (**theTE32KHandle).selPoint = selPt;
  582.         
  583.         TE32KCalText(theTE32KHandle);
  584.     }
  585. }
  586.  
  587.  
  588.  
  589. Handle    TE32KGetText(TE32KHandle theTE32KHandle)
  590. {
  591.     if (theTE32KHandle) 
  592.     {
  593.         Handle hText = (**theTE32KHandle).hText;
  594.         HandToHand(&hText);
  595.         return hText;
  596.     } 
  597.     
  598.     else
  599.         return nil;
  600. }
  601.  
  602.  
  603.  
  604.  
  605. void    TE32KUpdate(LongRect *updateLongRect,TE32KHandle theTE32KHandle)
  606. {
  607. LongRect                    tempLongRect;
  608. Rect                        theClipRect,viewRect,updateRect;
  609. GrafPtr                        oldPort,currentPort;
  610. RgnHandle                    oldClipRgn;
  611. register unsigned char        *textPtr;
  612. register long                firstLine,lastLine,i,thisStart,nextStart,tabWidth;
  613. Point                        cursorPt;
  614. int                            oldFont,oldFace,oldSize,oldMode;
  615. int                            rightSide,destLeftSide;
  616. LongPoint                    selPt;
  617. unsigned char                oldCaretState;
  618.  
  619.  
  620.     if (theTE32KHandle && (**theTE32KHandle).inPort)
  621.     {
  622.         tempLongRect = (**theTE32KHandle).viewRect;
  623.         LongRectToRect(&tempLongRect,&viewRect);
  624.         
  625.         tempLongRect = *updateLongRect;
  626.         tempLongRect.top = (**theTE32KHandle).destRect.top + ((updateLongRect->top - (**theTE32KHandle).destRect.top)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight;
  627.         tempLongRect.bottom = (**theTE32KHandle).destRect.top + ((updateLongRect->bottom - (**theTE32KHandle).destRect.top + (**theTE32KHandle).lineHeight - 1L)/(**theTE32KHandle).lineHeight)*(**theTE32KHandle).lineHeight;
  628.         
  629.         LongRectToRect(&tempLongRect,&updateRect);
  630.         
  631.         if (SectRect(&viewRect,&updateRect,&theClipRect))
  632.         {
  633.             GetPort(&oldPort);
  634.             currentPort = (**theTE32KHandle).inPort;
  635.             SetPort(currentPort);
  636.             
  637.             oldClipRgn = NewRgn();
  638.             GetClip(oldClipRgn);
  639.             ClipRect(&theClipRect);
  640.             
  641.             oldCaretState = (**theTE32KHandle).caretState;
  642.             
  643.             if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && oldCaretState)
  644.                 xorCaret(theTE32KHandle);
  645.             
  646.             firstLine = ((long) theClipRect.top - (**theTE32KHandle).destRect.top)/(long) (**theTE32KHandle).lineHeight;
  647.             lastLine = ((long) theClipRect.bottom - (**theTE32KHandle).destRect.top - 1L)/(long) (**theTE32KHandle).lineHeight;
  648.             
  649.             if (firstLine < 0)
  650.                 firstLine = 0;
  651.             
  652.             if (lastLine >= (**theTE32KHandle).nLines)
  653.                 lastLine = (**theTE32KHandle).nLines - 1L;
  654.             
  655.             if (firstLine > lastLine)
  656.                 lastLine = firstLine;
  657.             
  658.             EraseRect(&theClipRect);
  659.             
  660.             if (firstLine < (**theTE32KHandle).nLines && (**theTE32KHandle).teLength > 0L)
  661.             {
  662.                 rightSide = theClipRect.right;
  663.                 destLeftSide = (int) (**theTE32KHandle).destRect.left + 1L;
  664.                 cursorPt.h = destLeftSide;
  665.                 cursorPt.v = (int) ((**theTE32KHandle).destRect.top + firstLine * (long) (**theTE32KHandle).lineHeight + (long) (**theTE32KHandle).fontAscent);
  666.                 
  667.                 oldFont = ((**theTE32KHandle).inPort)->txFont;
  668.                 oldFace = ((**theTE32KHandle).inPort)->txFace;
  669.                 oldSize = ((**theTE32KHandle).inPort)->txSize;
  670.                 oldMode = ((**theTE32KHandle).inPort)->txMode;
  671.                 
  672.                 TextFont((**theTE32KHandle).txFont);
  673.                 TextFace((**theTE32KHandle).txFace);
  674.                 TextSize((**theTE32KHandle).txSize);
  675.                 TextMode((**theTE32KHandle).txMode);
  676.                 
  677.                 HLock((**theTE32KHandle).hText);
  678.                 
  679.                 textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  680.                 tabWidth = (long) (**theTE32KHandle).tabWidth;
  681.                 
  682.                 while (firstLine <= lastLine)
  683.                 {
  684.                     thisStart = (**theTE32KHandle).lineStarts[firstLine];
  685.                     i = thisStart;
  686.                     
  687.                     nextStart = (**theTE32KHandle).lineStarts[firstLine+1];
  688.                     
  689.                     if (nextStart > thisStart && (textPtr[nextStart-1] == '\r' || textPtr[nextStart-1] == '\n'))
  690.                         nextStart--;
  691.                     
  692.                     MoveTo(cursorPt.h,cursorPt.v);
  693.                     
  694.                     while (thisStart < nextStart)
  695.                     {
  696.                         while (i<nextStart && textPtr[i]!=TAB)
  697.                             i++;
  698.                         
  699.                         if (i > thisStart)
  700.                             DrawText(&(textPtr[thisStart]),0,(int) (i - thisStart));
  701.                         
  702.                         if (i<nextStart && textPtr[i]==TAB)
  703.                         {
  704.                             MoveTo(destLeftSide + ((currentPort->pnLoc.h - destLeftSide + tabWidth)/tabWidth)*tabWidth,currentPort->pnLoc.v);
  705.                             i++;
  706.                         }
  707.                         
  708.                         thisStart = i;
  709.                         
  710.                         if (currentPort->pnLoc.h > theClipRect.right)
  711.                             thisStart = nextStart;
  712.                     }
  713.                     
  714.                     firstLine++;
  715.                     cursorPt.v += (**theTE32KHandle).lineHeight;
  716.                 }
  717.                 
  718.                 HUnlock((**theTE32KHandle).hText);
  719.                 
  720.                 TextFont(oldFont);
  721.                 TextFace(oldFace);
  722.                 TextSize(oldSize);
  723.                 TextMode(oldMode);
  724.             }
  725.             
  726.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  727.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  728.             
  729.             else
  730.             {
  731.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  732.                 (**theTE32KHandle).selPoint = selPt;
  733.                 
  734.                 if (oldCaretState)
  735.                     xorCaret(theTE32KHandle);
  736.             }
  737.             
  738.             SetClip(oldClipRgn);
  739.             DisposeRgn(oldClipRgn);
  740.             
  741.             SetPort(oldPort);
  742.         }
  743.     }
  744. }
  745.  
  746.  
  747.  
  748.  
  749.  
  750. void    TE32KScroll(long horiz,long vert,TE32KHandle theTE32KHandle)
  751. {
  752. LongRect    updateLongRect;
  753. Rect        scrollRect;
  754. RgnHandle    updateRgn;
  755. GrafPtr        oldPort;
  756. LongPoint    selPt;
  757.  
  758.     if (theTE32KHandle && (**theTE32KHandle).inPort && (horiz || vert))
  759.     {
  760.         GetPort(&oldPort);
  761.         SetPort((**theTE32KHandle).inPort);
  762.         
  763.         (**theTE32KHandle).destRect.left += horiz;
  764.         (**theTE32KHandle).destRect.top += vert;
  765.         (**theTE32KHandle).destRect.right += horiz;
  766.         (**theTE32KHandle).destRect.bottom += vert;
  767.         
  768.         (**theTE32KHandle).selPoint.h += horiz;
  769.         (**theTE32KHandle).selPoint.v += vert;
  770.         selPt = (**theTE32KHandle).selPoint;
  771.         
  772.         scrollRect.left = (int) ((**theTE32KHandle).viewRect.left);
  773.         scrollRect.top = (int) ((**theTE32KHandle).viewRect.top);
  774.         scrollRect.right = (int) ((**theTE32KHandle).viewRect.right);
  775.         scrollRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  776.         
  777.         if (horiz < ((**theTE32KHandle).viewRect.right-(**theTE32KHandle).viewRect.left) ||
  778.             vert < ((**theTE32KHandle).viewRect.bottom-(**theTE32KHandle).viewRect.top))
  779.         {
  780.             updateRgn = NewRgn();
  781.             
  782.             ScrollRect(&scrollRect,(int) horiz,(int) vert,updateRgn);
  783.             
  784.             updateLongRect.left = (**updateRgn).rgnBBox.left;
  785.             updateLongRect.top = (**updateRgn).rgnBBox.top;
  786.             updateLongRect.right = (**updateRgn).rgnBBox.right;
  787.             updateLongRect.bottom = (**updateRgn).rgnBBox.bottom;
  788.             
  789.             DisposeRgn(updateRgn);
  790.             
  791.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  792.             
  793.             if ((**theTE32KHandle).caretState)
  794.                 xorCaret(theTE32KHandle);
  795.             
  796.             (**theTE32KHandle).selPoint = selPt;
  797.             
  798.             xorCaret(theTE32KHandle);
  799.         }
  800.         
  801.         else
  802.         {
  803.             updateLongRect = (**theTE32KHandle).viewRect;
  804.             
  805.             TE32KUpdate(&updateLongRect,theTE32KHandle);
  806.             
  807.             if ((**theTE32KHandle).caretState)
  808.                 xorCaret(theTE32KHandle);
  809.             
  810.             (**theTE32KHandle).selPoint = selPt;
  811.             
  812.             xorCaret(theTE32KHandle);
  813.         }
  814.         
  815.         SetPort(oldPort);
  816.     }
  817. }
  818.  
  819.  
  820.  
  821.  
  822.  
  823. void    TE32KActivate(TE32KHandle theTE32KHandle)
  824. {
  825.     if (theTE32KHandle && !((**theTE32KHandle).active))
  826.     {
  827.         (**theTE32KHandle).active = TRUE;
  828.         (**theTE32KHandle).caretState = FALSE;
  829.         
  830.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  831.     }
  832. }
  833.  
  834.  
  835.  
  836.  
  837.  
  838. void    TE32KIdle(TE32KHandle theTE32KHandle)
  839. {
  840.     if (theTE32KHandle && (**theTE32KHandle).active && TickCount() >= (**theTE32KHandle).caretTime)
  841.     {
  842.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  843.             xorCaret(theTE32KHandle);
  844.     }
  845. }
  846.  
  847.  
  848.  
  849.  
  850.  
  851. void    TE32KDeactivate(TE32KHandle theTE32KHandle)
  852. {
  853.     if (theTE32KHandle && (**theTE32KHandle).active)
  854.     {
  855.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  856.         {
  857.             if ((**theTE32KHandle).caretState)
  858.                 xorCaret(theTE32KHandle);
  859.         }
  860.         else
  861.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  862.         
  863.         (**theTE32KHandle).active = FALSE;
  864.     }
  865. }
  866.  
  867.  
  868.  
  869. void    TE32KGetPoint(long selIndex,LongPoint *selPt,TE32KHandle theTE32KHandle)
  870. {
  871. register unsigned char    *textPtr;
  872. register int            *theCharWidths;
  873. register long            i,thisStart,tabWidth;
  874. long                    x,y,lineIndex,destLeftSide;
  875. unsigned char            ch;
  876. LongPoint                origPt;
  877. int                        clikStuff;
  878.  
  879.     if (theTE32KHandle)
  880.     {
  881.         if (selIndex<=0L || (**theTE32KHandle).teLength<1L)
  882.         {
  883.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  884.             selPt->v = (**theTE32KHandle).destRect.top + (**theTE32KHandle).fontAscent;
  885.             (**theTE32KHandle).clikStuff = FALSE;
  886.             
  887.             return;
  888.         }
  889.         
  890.         clikStuff = (**theTE32KHandle).clikStuff;
  891.         (**theTE32KHandle).clikStuff = FALSE;
  892.         
  893.         origPt = *selPt;
  894.         
  895.         i = indexToLine(selIndex,theTE32KHandle);
  896.         
  897.         y = (**theTE32KHandle).destRect.top + ((**theTE32KHandle).lineHeight * i) + (**theTE32KHandle).fontAscent;
  898.         
  899.         selPt->v = y;
  900.         
  901.         if (!(**theTE32KHandle).crOnly && clikStuff && i > 0 && selIndex == (**theTE32KHandle).lineStarts[i])
  902.         {
  903.             i--;
  904.             selPt->v -= (**theTE32KHandle).lineHeight;
  905.         }
  906.         
  907.         else if (selIndex < (**theTE32KHandle).lineStarts[i] || (selIndex == (**theTE32KHandle).lineStarts[i] && i < 1))
  908.         {
  909.             selPt->h = (**theTE32KHandle).destRect.left + 1L;
  910.             return;
  911.         }
  912.         
  913.         
  914.         HLock((**theTE32KHandle).hText);
  915.         
  916.         lineIndex  = i;
  917.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  918.         
  919.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  920.         x = destLeftSide;
  921.         
  922.         thisStart = (**theTE32KHandle).lineStarts[lineIndex];
  923.         
  924.         theCharWidths = (**theTE32KHandle).theCharWidths;
  925.         
  926.         if (textPtr[selIndex-1] != '\r' && textPtr[selIndex-1] != '\n')
  927.         {
  928.             tabWidth = (long) (**theTE32KHandle).tabWidth;
  929.             
  930.             while (thisStart < selIndex)
  931.             {
  932.                 ch = textPtr[thisStart++];
  933.                 
  934.                 if (ch == TAB)
  935.                     x = destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  936.                 
  937.                 else
  938.                     x += theCharWidths[ch];
  939.             }
  940.         }
  941.         
  942.         HUnlock((**theTE32KHandle).hText);
  943.         
  944.         selPt->h = x;
  945.     }
  946. }
  947.  
  948.  
  949.  
  950.  
  951. long    TE32KGetOffset(LongPoint *selPt,TE32KHandle theTE32KHandle)
  952. {
  953. register unsigned char    *textPtr;
  954. register int            *theCharWidths;
  955. register long            i,delta,firstChar,lastChar,tabWidth;
  956. unsigned char            done;
  957. long                    x,y,selIndex,horiz,destLeftSide;
  958.  
  959.     if (theTE32KHandle)
  960.     {
  961.         if ((**theTE32KHandle).teLength < 1L)
  962.             return(0L);
  963.         
  964.         horiz = selPt->h;
  965.         
  966.         y = selPt->v - (**theTE32KHandle).destRect.top;
  967.         
  968.         i = y / (long) (**theTE32KHandle).lineHeight;
  969.         
  970.         if (i < 0L)
  971.             return(0L);
  972.         
  973.         if (i >= (**theTE32KHandle).nLines)
  974.             return((**theTE32KHandle).teLength);
  975.         
  976.         theCharWidths = (**theTE32KHandle).theCharWidths;
  977.         
  978.         HLock((**theTE32KHandle).hText);
  979.         
  980.         textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  981.         
  982.         destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  983.         x = destLeftSide;
  984.         delta = 0L;
  985.         
  986.         firstChar = (**theTE32KHandle).lineStarts[i];
  987.         lastChar = (**theTE32KHandle).lineStarts[i+1L];
  988.         
  989.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  990.         
  991.         if (firstChar<lastChar && x+delta<horiz)
  992.         {
  993.             done = FALSE;
  994.         
  995.             while (!done)
  996.             {
  997.                 if (textPtr[firstChar] != TAB)
  998.                     delta = (long) theCharWidths[textPtr[firstChar]];
  999.                 
  1000.                 else
  1001.                     delta = (destLeftSide + ((x - destLeftSide + tabWidth)/tabWidth)*tabWidth) - x;
  1002.                 
  1003.                 firstChar++;
  1004.                 
  1005.                 if (firstChar >= lastChar)
  1006.                 {    
  1007.                     if (textPtr[lastChar - 1L] == '\r' || textPtr[lastChar - 1L] == '\n')
  1008.                         selIndex = lastChar - 1L;
  1009.                     else
  1010.                         selIndex = lastChar;
  1011.                     
  1012.                     done = TRUE;
  1013.                 }
  1014.                 
  1015.                 else if (x+delta >= horiz)
  1016.                 {
  1017.                     if (horiz >= x + (delta >> 1))
  1018.                         selIndex = firstChar;
  1019.                     else
  1020.                         selIndex = --firstChar;
  1021.                     
  1022.                     done = TRUE;
  1023.                 }
  1024.                 
  1025.                 else
  1026.                     x += delta;
  1027.             }
  1028.         }
  1029.         
  1030.         else
  1031.             selIndex = firstChar;
  1032.  
  1033.         HUnlock((**theTE32KHandle).hText);
  1034.         
  1035.         return(selIndex);
  1036.     }
  1037. }
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045. static void    invertSelRange(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1046. {
  1047. long        firstLine,lastLine,oldMode;
  1048. Rect        viewRect,tempRect1,tempRect2,tempRect3,theRect;
  1049. LongPoint    selPt;
  1050. GrafPtr        oldPort;
  1051.  
  1052.  
  1053.     if (theTE32KHandle && (**theTE32KHandle).active)
  1054.     {
  1055.         if ((**theTE32KHandle).caretState)
  1056.             xorCaret(theTE32KHandle);
  1057.         
  1058.         if (selStart == selEnd)
  1059.         {
  1060.             TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1061.             (**theTE32KHandle).selPoint = selPt;
  1062.             (**theTE32KHandle).selStart = selStart;
  1063.             (**theTE32KHandle).selEnd = selStart;
  1064.             
  1065.             xorCaret(theTE32KHandle);
  1066.             
  1067.             return;
  1068.         }
  1069.         
  1070.         
  1071.         viewRect.left = (int) ((**theTE32KHandle).viewRect.left);
  1072.         viewRect.top = (int) ((**theTE32KHandle).viewRect.top);
  1073.         viewRect.right = (int) ((**theTE32KHandle).viewRect.right);
  1074.         viewRect.bottom = (int) ((**theTE32KHandle).viewRect.bottom);
  1075.         
  1076.         GetPort(&oldPort);
  1077.         SetPort((**theTE32KHandle).inPort);
  1078.         
  1079.         if (selStart > selEnd)
  1080.         {
  1081.             firstLine = selStart;
  1082.             selStart = selEnd;
  1083.             selEnd = firstLine;
  1084.         }
  1085.         
  1086.         firstLine = indexToLine(selStart,theTE32KHandle);
  1087.         lastLine = indexToLine(selEnd,theTE32KHandle);
  1088.         
  1089.         
  1090.         TE32KGetPoint(selStart,&selPt,theTE32KHandle);
  1091.         
  1092.         selPt.v -= (**theTE32KHandle).fontAscent;
  1093.         
  1094.         if (selStart <= (**theTE32KHandle).lineStarts[firstLine])
  1095.             selPt.h--;
  1096.         
  1097.         if (selPt.h < -32768L)
  1098.             tempRect1.left = (int) -32768;
  1099.         else if (selPt.h > 32767L)
  1100.             tempRect1.left = (int) 32767;
  1101.         else
  1102.             tempRect1.left = (int) selPt.h;
  1103.         
  1104.         if (selPt.v < -32768L)
  1105.             tempRect1.top = (int) -32768;
  1106.         else if (selPt.v > 32767L)
  1107.             tempRect1.top = (int) 32767;
  1108.         else
  1109.             tempRect1.top = (int) selPt.v;
  1110.         
  1111.         
  1112.         if (firstLine != lastLine)
  1113.         {
  1114.             tempRect1.right = viewRect.right;
  1115.             tempRect1.bottom = tempRect1.top + (**theTE32KHandle).lineHeight;
  1116.         }
  1117.         
  1118.         else
  1119.         {
  1120.             TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1121.             
  1122.             selPt.v -= (**theTE32KHandle).fontAscent;
  1123.             selPt.v += (**theTE32KHandle).lineHeight;
  1124.             
  1125.             if (selPt.h < -32768L)
  1126.                 tempRect1.right = (int) -32768;
  1127.             else if (selPt.h > 32767L)
  1128.                 tempRect1.right = (int) 32767;
  1129.             else
  1130.                 tempRect1.right = (int) selPt.h;
  1131.             
  1132.             if (selPt.v < -32768L)
  1133.                 tempRect1.bottom = (int) -32768;
  1134.             else if (selPt.v > 32767L)
  1135.                 tempRect1.bottom = (int) 32767;
  1136.             else
  1137.                 tempRect1.bottom = (int) selPt.v;
  1138.         }
  1139.         
  1140.         if (SectRect(&viewRect,&tempRect1,&theRect))
  1141.         {
  1142.             oldMode = HiliteMode;
  1143.             HiliteMode = 1;
  1144.             InvertRect(&theRect);
  1145.             HiliteMode = oldMode;
  1146.         }
  1147.         
  1148.  
  1149.         
  1150.         if (lastLine > firstLine + 1L)
  1151.         {
  1152.             TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1153.             
  1154.             tempRect2.left = viewRect.left;
  1155.             tempRect2.top = tempRect1.bottom;
  1156.             tempRect2.right = viewRect.right;
  1157.             
  1158.             selPt.v -= (**theTE32KHandle).fontAscent;
  1159.             
  1160.             if (selPt.v < -32768L)
  1161.                 tempRect2.bottom = (int) -32768;
  1162.             else if (selPt.v > 32767L)
  1163.                 tempRect2.bottom = (int) 32767;
  1164.             else
  1165.                 tempRect2.bottom = (int) selPt.v;
  1166.             
  1167.             selPt.v += (**theTE32KHandle).fontAscent;
  1168.             
  1169.             if (SectRect(&viewRect,&tempRect2,&theRect))
  1170.             {
  1171.                 oldMode = HiliteMode;
  1172.                 HiliteMode = 1;
  1173.                 InvertRect(&theRect);
  1174.                 HiliteMode = oldMode;
  1175.             }
  1176.         }
  1177.         
  1178.         if (lastLine > firstLine && selEnd > (**theTE32KHandle).lineStarts[lastLine])
  1179.         {
  1180.             if (lastLine == firstLine + 1L)
  1181.             {
  1182.                 TE32KGetPoint(selEnd,&selPt,theTE32KHandle);
  1183.             }
  1184.             
  1185.             selPt.v -= (**theTE32KHandle).fontAscent;
  1186.             
  1187.             if (selPt.v < -32768L)
  1188.                 tempRect3.top = (int) -32768;
  1189.             else if (selPt.v > 32767L)
  1190.                 tempRect3.top = (int) 32767;
  1191.             else
  1192.                 tempRect3.top = (int) selPt.v;
  1193.             
  1194.             selPt.v += (**theTE32KHandle).lineHeight;
  1195.             
  1196.             if (selPt.v < -32768L)
  1197.                 tempRect3.bottom = (int) -32768;
  1198.             else if (selPt.v > 32767L)
  1199.                 tempRect3.bottom = (int) 32767;
  1200.             else
  1201.                 tempRect3.bottom = (int) selPt.v;
  1202.             
  1203.             
  1204.             tempRect3.left = viewRect.left;
  1205.             
  1206.             if (selPt.h < -32768L)
  1207.                 tempRect3.right = (int) -32768;
  1208.             else if (selPt.h > 32767L)
  1209.                 tempRect3.right = (int) 32767;
  1210.             else
  1211.                 tempRect3.right = (int) selPt.h;
  1212.             
  1213.             
  1214.             if (SectRect(&viewRect,&tempRect3,&theRect))
  1215.             {
  1216.                 oldMode = HiliteMode;
  1217.                 HiliteMode = 1;
  1218.                 InvertRect(&theRect);
  1219.                 HiliteMode = oldMode;
  1220.             }
  1221.         }
  1222.         
  1223.         
  1224.         SetPort(oldPort);
  1225.     }
  1226. }
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232. void    TE32KClick(Point startPoint,unsigned char extend,TE32KHandle theTE32KHandle)
  1233. {
  1234. register long                selIndex,selAnchor,selLast,teLength;
  1235. register unsigned char        *textPtr;
  1236. LongPoint                    selPt,tempPt;
  1237. Point                        mousePt;
  1238. GrafPtr                        oldPort;
  1239. unsigned char                ch;
  1240. long                        oldClickLoc,wordStart,wordEnd;
  1241.  
  1242.  
  1243.     if (theTE32KHandle && (**theTE32KHandle).active)
  1244.     {
  1245.         clickedTE32KH = theTE32KHandle;
  1246.         
  1247.         teLength = (**theTE32KHandle).teLength;
  1248.         
  1249.         selPt.h = (long) startPoint.h;
  1250.         selPt.v = (long) startPoint.v;
  1251.         
  1252.         selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1253.         
  1254.         oldClickLoc = (**theTE32KHandle).clickLoc;
  1255.         (**theTE32KHandle).clickLoc = selIndex;
  1256.  
  1257.         if ((**theTE32KHandle).caretState)
  1258.             xorCaret(theTE32KHandle);
  1259.         
  1260.         if (!extend && teLength > 0L && TickCount() < (**theTE32KHandle).clickTime + GetDblTime() && oldClickLoc == selIndex)
  1261.         {
  1262.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1263.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1264.             
  1265.             selAnchor = selIndex;
  1266.             selIndex = selAnchor;
  1267.             textPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1268.             
  1269.             while (selIndex > 0L)
  1270.             {
  1271.                 ch = textPtr[selIndex-1L];
  1272.                 
  1273.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1274.                     selIndex--;
  1275.                 else
  1276.                     break;
  1277.             }
  1278.             
  1279.             if (selIndex < 0L)
  1280.                 (**theTE32KHandle).selStart =  0L;
  1281.             else
  1282.                 (**theTE32KHandle).selStart = selIndex;
  1283.             
  1284.             wordStart = (**theTE32KHandle).selStart;
  1285.             selIndex = selAnchor;
  1286.             teLength = (**theTE32KHandle).teLength;
  1287.             
  1288.             while (selIndex < teLength)
  1289.             {
  1290.                 ch = textPtr[selIndex];
  1291.                 
  1292.                 if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1293.                     selIndex++;
  1294.                 else
  1295.                     break;
  1296.             }
  1297.             
  1298.             if (selIndex > teLength)
  1299.                 (**theTE32KHandle).selEnd =  teLength;
  1300.             else
  1301.                 (**theTE32KHandle).selEnd = selIndex;
  1302.             
  1303.             wordEnd = (**theTE32KHandle).selEnd;
  1304.             
  1305.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1306.             
  1307.             while (Button()) 
  1308.             {
  1309.                 if ((**theTE32KHandle).clikLoop)
  1310.                     (*((**theTE32KHandle).clikLoop)) ();
  1311.                 
  1312.                 GetMouse(&mousePt);
  1313.                 selPt.h = mousePt.h;
  1314.                 selPt.v = mousePt.v;
  1315.             
  1316.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1317.                 
  1318.                 if (selIndex < 0)
  1319.                     selIndex = 0;
  1320.                 
  1321.                 else if (selIndex > teLength)
  1322.                     selIndex = teLength;
  1323.                 
  1324.                 if (selIndex < wordStart)
  1325.                     while (selIndex > 0) 
  1326.                     {
  1327.                         ch = textPtr[selIndex - 1];
  1328.                         
  1329.                         if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1330.                             selIndex--;
  1331.                         else
  1332.                             break;
  1333.                     }
  1334.                 
  1335.                 else if (selIndex > wordEnd)
  1336.                     while (selIndex < teLength) 
  1337.                     {
  1338.                         ch = textPtr[selIndex];
  1339.                         
  1340.                         if ((ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'))
  1341.                             selIndex++;
  1342.                         else
  1343.                             break;
  1344.                     }
  1345.             
  1346.                 if (selIndex < wordEnd && wordEnd < (**theTE32KHandle).selEnd) 
  1347.                 {
  1348.                     invertSelRange(wordEnd, (**theTE32KHandle).selEnd, theTE32KHandle);
  1349.                     (**theTE32KHandle).selEnd = wordEnd;
  1350.                 }
  1351.             
  1352.                 if (selIndex > wordStart && wordStart > (**theTE32KHandle).selStart) 
  1353.                 {
  1354.                     invertSelRange((**theTE32KHandle).selStart, wordStart, theTE32KHandle);
  1355.                     (**theTE32KHandle).selStart = wordStart;
  1356.                 }
  1357.             
  1358.                 if (selIndex < (**theTE32KHandle).selStart || (selIndex > (**theTE32KHandle).selStart && selIndex < wordStart)) 
  1359.                 {
  1360.                     invertSelRange(selIndex, (**theTE32KHandle).selStart, theTE32KHandle);
  1361.                     (**theTE32KHandle).selStart = selIndex;
  1362.                 } 
  1363.                 
  1364.                 else if (selIndex > (**theTE32KHandle).selEnd || (selIndex < (**theTE32KHandle).selEnd && selIndex > wordEnd)) 
  1365.                 {
  1366.                     invertSelRange((**theTE32KHandle).selEnd, selIndex, theTE32KHandle);
  1367.                     (**theTE32KHandle).selEnd = selIndex;
  1368.                 }
  1369.             }
  1370.         }
  1371.         
  1372.         else
  1373.         {
  1374.             if (!extend)
  1375.             {
  1376.                 if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1377.                     invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1378.                 
  1379.                 (**theTE32KHandle).selStart = selIndex;
  1380.                 (**theTE32KHandle).selEnd = selIndex;
  1381.                     
  1382.                 (**theTE32KHandle).clikStuff = FALSE;
  1383.                 TE32KGetPoint(selIndex,&selPt,theTE32KHandle);
  1384.                 
  1385.                 if ((**theTE32KHandle).crOnly)
  1386.                     (**theTE32KHandle).selPoint = selPt;
  1387.                 
  1388.                 else
  1389.                 {
  1390.                     (**theTE32KHandle).clikStuff = TRUE;
  1391.                     TE32KGetPoint(selIndex,&tempPt,theTE32KHandle);
  1392.                     
  1393.                     if ((selPt.h - startPoint.h)*(selPt.h - startPoint.h) + (selPt.v - startPoint.v)*(selPt.v - startPoint.v) <
  1394.                         (tempPt.h - startPoint.h)*(tempPt.h - startPoint.h) + (tempPt.v - startPoint.v)*(tempPt.v - startPoint.v))
  1395.                             (**theTE32KHandle).selPoint = selPt;
  1396.                     else
  1397.                             (**theTE32KHandle).selPoint = tempPt;
  1398.                 }
  1399.                 
  1400.                 xorCaret(theTE32KHandle);
  1401.             }
  1402.                 
  1403.                 
  1404.             
  1405.             if (extend || StillDown())
  1406.             {
  1407.                 if (extend)
  1408.                 {
  1409.                     if (selIndex >= (**theTE32KHandle).selEnd)
  1410.                     {
  1411.                         selAnchor = (**theTE32KHandle).selStart;
  1412.                         selLast = (**theTE32KHandle).selEnd;
  1413.                     }
  1414.                     else
  1415.                     {
  1416.                         selAnchor = (**theTE32KHandle).selEnd;
  1417.                         selLast = (**theTE32KHandle).selStart;
  1418.                     }
  1419.                 }
  1420.                 else
  1421.                 {
  1422.                     selAnchor = selIndex;
  1423.                     selLast = selIndex;
  1424.                 }
  1425.                 
  1426.                 GetPort(&oldPort);
  1427.                 SetPort((**theTE32KHandle).inPort);
  1428.                 
  1429.                 if (extend)
  1430.                     goto DOHILITE;
  1431.                 
  1432.                 while (StillDown())
  1433.                 {
  1434.                     if (selIndex >= selAnchor)
  1435.                     {
  1436.                         (**theTE32KHandle).selStart = selAnchor;
  1437.                         (**theTE32KHandle).selEnd = selIndex;
  1438.                     }
  1439.                     else
  1440.                     {
  1441.                         (**theTE32KHandle).selStart = selIndex;
  1442.                         (**theTE32KHandle).selEnd = selAnchor;
  1443.                     }
  1444.                     
  1445.                     if ((**theTE32KHandle).clikLoop)
  1446.                         (*((**theTE32KHandle).clikLoop)) ();
  1447.                     
  1448.                     GetMouse(&mousePt);
  1449.                     
  1450.                     selPt.h = (long) mousePt.h;
  1451.                     selPt.v = (long) mousePt.v;
  1452.                     
  1453.                     selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  1454.     
  1455.     DOHILITE:
  1456.                     if (selLast >= selAnchor)
  1457.                     {
  1458.                         if (selIndex > selLast)
  1459.                             invertSelRange(selLast,selIndex,theTE32KHandle);
  1460.                         
  1461.                         else if (selIndex>=selAnchor && selIndex<selLast)
  1462.                             invertSelRange(selIndex,selLast,theTE32KHandle);
  1463.                         
  1464.                         else if (selIndex<selAnchor)
  1465.                         {
  1466.                             invertSelRange(selAnchor,selLast,theTE32KHandle);
  1467.                             invertSelRange(selIndex,selAnchor,theTE32KHandle);
  1468.                         }
  1469.                     }
  1470.                     
  1471.                     else
  1472.                     {
  1473.                         if (selIndex < selLast)
  1474.                             invertSelRange(selIndex,selLast,theTE32KHandle);
  1475.                         
  1476.                         else if (selIndex<=selAnchor && selIndex>selLast)
  1477.                             invertSelRange(selLast,selIndex,theTE32KHandle);
  1478.                         
  1479.                         else if (selIndex>selAnchor)
  1480.                         {
  1481.                             invertSelRange(selLast,selAnchor,theTE32KHandle);
  1482.                             invertSelRange(selAnchor,selIndex,theTE32KHandle);
  1483.                         }
  1484.                     }
  1485.                     
  1486.                     selLast = selIndex;
  1487.                 }
  1488.                 
  1489.                 SetPort(oldPort);
  1490.                 
  1491.                 
  1492.                 if (selIndex >= selAnchor)
  1493.                 {
  1494.                     (**theTE32KHandle).selStart = selAnchor;
  1495.                     (**theTE32KHandle).selEnd = selIndex;
  1496.                 }
  1497.                 else
  1498.                 {
  1499.                     (**theTE32KHandle).selStart = selIndex;
  1500.                     (**theTE32KHandle).selEnd = selAnchor;
  1501.                 }
  1502.             }
  1503.         }
  1504.         
  1505.         (**theTE32KHandle).clickTime = TickCount();
  1506.         
  1507.         clickedTE32KH = 0L;
  1508.     }
  1509. }
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515. void    TE32KSetSelect(long selStart,long selEnd,TE32KHandle theTE32KHandle)
  1516. {
  1517.     if (theTE32KHandle)
  1518.     {
  1519.         if ((**theTE32KHandle).active)
  1520.         {
  1521.             if ((**theTE32KHandle).selStart != (**theTE32KHandle).selEnd)
  1522.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1523.             
  1524.             else if ((**theTE32KHandle).caretState)
  1525.                 xorCaret(theTE32KHandle);
  1526.         }
  1527.                 
  1528.         if (selStart <= selEnd)
  1529.         {
  1530.             (**theTE32KHandle).selStart = selStart;
  1531.             (**theTE32KHandle).selEnd = selEnd;
  1532.         }
  1533.         else
  1534.         {
  1535.             (**theTE32KHandle).selStart = selEnd;
  1536.             (**theTE32KHandle).selEnd = selStart;
  1537.         }
  1538.         
  1539.         if ((**theTE32KHandle).active)
  1540.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1541.     }
  1542. }
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548. OSErr TE32KToScrap(void)
  1549. {
  1550. OSErr err;
  1551.  
  1552.     if (TE32KScrpHandle && TE32KScrpLength > 0L)
  1553.     {
  1554.         if ((err = ZeroScrap()) != noErr)
  1555.             return(err);
  1556.  
  1557.         HLockHi(TE32KScrpHandle);
  1558.  
  1559.         if ((err = PutScrap(TE32KScrpLength,'TEXT',(Ptr) *TE32KScrpHandle)) != noErr)
  1560.             return(err);
  1561.  
  1562.         HUnlock(TE32KScrpHandle);
  1563.  
  1564.         return(noErr);
  1565.     }
  1566.     
  1567.     return(noScrapErr);
  1568. }
  1569.  
  1570.  
  1571.  
  1572.  
  1573. OSErr TE32KFromScrap(void)
  1574. {
  1575. long offset;
  1576. OSErr err;
  1577.  
  1578.     if (TE32KScrpHandle && (err = GetScrap(0L,'TEXT',&offset)) > 0L)
  1579.     {
  1580.         TE32KScrpLength = GetScrap(TE32KScrpHandle,'TEXT',&offset);
  1581.         
  1582.         if (TE32KScrpLength > 0L)
  1583.             return(noErr);
  1584.         
  1585.         else if (TE32KScrpLength == 0L)
  1586.             return(noTypeErr);
  1587.         
  1588.         else
  1589.             return(TE32KScrpLength);
  1590.     }
  1591.     
  1592.     if (TE32KScrpHandle == NULL)
  1593.         return(noScrapErr);
  1594.     
  1595.     else
  1596.         return(err);
  1597. }
  1598.  
  1599.  
  1600.  
  1601.  
  1602. void    TE32KCopy(TE32KHandle theTE32KHandle)
  1603. {
  1604.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1605.     {
  1606.         SetHandleSize(TE32KScrpHandle,(**theTE32KHandle).selEnd - (**theTE32KHandle).selStart);
  1607.         
  1608.         if (!MemError() && GetHandleSize(TE32KScrpHandle) >= ((**theTE32KHandle).selEnd - (**theTE32KHandle).selStart))
  1609.         {
  1610.             TE32KScrpLength = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1611.             
  1612.             HLock(TE32KScrpHandle);
  1613.             HLock((**theTE32KHandle).hText);
  1614.             
  1615.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*TE32KScrpHandle,TE32KScrpLength);
  1616.             
  1617.             HUnlock((**theTE32KHandle).hText);
  1618.             HUnlock(TE32KScrpHandle);
  1619.         }
  1620.     }
  1621. }
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627. void    TE32KCut(TE32KHandle theTE32KHandle)
  1628. {
  1629.     if (theTE32KHandle && TE32KScrpHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1630.     {
  1631.         TE32KCopy(theTE32KHandle);
  1632.         TE32KDelete(theTE32KHandle);
  1633.     }
  1634. }
  1635.  
  1636.  
  1637.  
  1638. void    TE32KDelete(TE32KHandle theTE32KHandle)
  1639. {
  1640. LongRect            updateRect;
  1641. register long        *theLine,*otherLine,theLineStart,i,delta;
  1642. long                firstLine,lastLine;
  1643. Rect                tempRect;
  1644. RgnHandle            updateRgn;
  1645. GrafPtr                oldPort;
  1646.  
  1647.  
  1648.     if (theTE32KHandle && (**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1649.     {
  1650.         invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1651.                 
  1652.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1653.         lastLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  1654.         
  1655.         updateRect = (**theTE32KHandle).viewRect;
  1656.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1657.         LongRectToRect(&updateRect,&tempRect);
  1658.         
  1659.         GetPort(&oldPort);
  1660.         SetPort((**theTE32KHandle).inPort);
  1661.                 
  1662.         updateRgn = NewRgn();
  1663.         ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight * (lastLine - firstLine),updateRgn);
  1664.         tempRect = (**updateRgn).rgnBBox;
  1665.         DisposeRgn(updateRgn);
  1666.         
  1667.         SetPort(oldPort);
  1668.         
  1669.         if ((**theTE32KHandle).selEnd != (**theTE32KHandle).teLength)
  1670.         {
  1671.             HLock((**theTE32KHandle).hText);
  1672.             BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selEnd,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1673.             HUnlock((**theTE32KHandle).hText);
  1674.         }
  1675.         
  1676.         delta = (**theTE32KHandle).selEnd - (**theTE32KHandle).selStart;
  1677.         
  1678.         theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1679.         otherLine = &((**theTE32KHandle).lineStarts[lastLine + 1L]);
  1680.         i = (**theTE32KHandle).nLines - lastLine;
  1681.         
  1682.         while (i--)
  1683.         {
  1684.             theLineStart = *(otherLine++);
  1685.             theLineStart -= delta;
  1686.             *(theLine++) = theLineStart;
  1687.         }
  1688.         
  1689.         (**theTE32KHandle).teLength -= delta;
  1690.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1691.         (**theTE32KHandle).nLines -= (lastLine - firstLine);
  1692.         
  1693.         RectToLongRect(&tempRect,&updateRect);
  1694.         TE32KUpdate(&updateRect,theTE32KHandle);    /* update scrolled stuff */
  1695.         
  1696.         updateLine(firstLine,theTE32KHandle,TRUE,0L);
  1697.     }
  1698. }
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704. void    TE32KInsert(Ptr textPtr,register long textLength,TE32KHandle theTE32KHandle)
  1705. {
  1706. register long                *theLine,*otherLine,i,numCRs;
  1707. long                        firstLine,teLength,maxLineStarts,sizeTE32KHandle;
  1708. register unsigned char        *charPtr,*charBase;
  1709. RgnHandle                    updateRgn;
  1710. Rect                        tempRect;
  1711. GrafPtr                        oldPort;
  1712.  
  1713.  
  1714.     if (theTE32KHandle && textPtr && textLength > 0L)
  1715.     {
  1716.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1717.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  1718.         
  1719.         if (textLength == 1L)
  1720.         {
  1721.             TE32KKey(*((unsigned char *) textPtr),theTE32KHandle);
  1722.         }
  1723.         
  1724.         else
  1725.         {
  1726.             firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1727.             
  1728.             teLength = (**theTE32KHandle).teLength + textLength;
  1729.             
  1730.             if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  1731.             {
  1732.                 SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  1733.                 
  1734.                 if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength + EXTRATEXTBUFF)
  1735.                     return;
  1736.             }
  1737.             
  1738.             HLock((**theTE32KHandle).hText);
  1739.             
  1740.             if ((**theTE32KHandle).teLength - (**theTE32KHandle).selStart)
  1741.                 BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + textLength,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  1742.             
  1743.             BlockMove(textPtr,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,textLength);
  1744.             
  1745.             HUnlock((**theTE32KHandle).hText);
  1746.             
  1747.             i = textLength;
  1748.             numCRs = 0L;
  1749.             charPtr = (unsigned char *) textPtr;
  1750.             
  1751.             while (i--)
  1752.             {
  1753.                 if (*charPtr == '\r' || *charPtr == '\n')
  1754.                     numCRs++;
  1755.                 
  1756.                 charPtr++;
  1757.             }
  1758.             
  1759.             if (numCRs)
  1760.             {
  1761.                 sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  1762.                 maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1763.                 
  1764.                 if ((**theTE32KHandle).nLines + numCRs >= maxLineStarts)
  1765.                 {
  1766.                     sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + numCRs + EXTRALINESTARTS);
  1767.                     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  1768.                     
  1769.                     SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  1770.                     
  1771.                     if (MemError())
  1772.                         return;
  1773.                 }
  1774.                 
  1775.                 theLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  1776.                 otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + numCRs]);
  1777.                 i = (**theTE32KHandle).nLines - firstLine;
  1778.                 
  1779.                 while (i--)
  1780.                     *(otherLine--) = *(theLine--) + textLength;
  1781.                 
  1782.                 charPtr = (unsigned char *) (*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart) + 1L;
  1783.                 charBase = (unsigned char *) *((**theTE32KHandle).hText);
  1784.                 theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1785.                 i = numCRs;
  1786.                 
  1787.                 while (i--)
  1788.                 {
  1789.                     while (*charPtr != '\r' && *charPtr != '\n')
  1790.                         charPtr++;
  1791.                     
  1792.                     charPtr++;
  1793.                     
  1794.                     *theLine++ = charPtr - charBase;
  1795.                 }
  1796.                 
  1797.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  1798.                 tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + 1L) * (**theTE32KHandle).lineHeight;
  1799.                 
  1800.                 GetPort(&oldPort);
  1801.                 SetPort((**theTE32KHandle).inPort);
  1802.                 
  1803.                 updateRgn = NewRgn();
  1804.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * numCRs,updateRgn);
  1805.                 DisposeRgn(updateRgn);
  1806.                 
  1807.                 SetPort(oldPort);
  1808.             }
  1809.             
  1810.             else
  1811.             {
  1812.                 theLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1813.                 i = (**theTE32KHandle).nLines - firstLine;
  1814.                 
  1815.                 while (i--)
  1816.                     *(theLine++) += textLength;
  1817.             }
  1818.             
  1819.             
  1820.             (**theTE32KHandle).teLength = teLength;
  1821.             (**theTE32KHandle).selStart += textLength;
  1822.             (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1823.             (**theTE32KHandle).nLines += numCRs;
  1824.             
  1825.             do
  1826.             {
  1827.                 updateLine(firstLine,theTE32KHandle,TRUE,0L);
  1828.                 
  1829.                 if (numCRs)
  1830.                 {
  1831.                     theLine = (**theTE32KHandle).lineStarts;
  1832.                     charPtr = (unsigned char *) *((**theTE32KHandle).hText);
  1833.                     
  1834.                     do
  1835.                     {
  1836.                         firstLine++;
  1837.                         
  1838.                     } while (firstLine < (**theTE32KHandle).nLines && charPtr[theLine[firstLine] - 1L] != '\r' && charPtr[theLine[firstLine] - 1L] != '\n');
  1839.                     
  1840.                 }
  1841.                 
  1842.             } while (numCRs--);
  1843.         }
  1844.     }
  1845. }
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852. void    TE32KPaste(TE32KHandle theTE32KHandle)
  1853. {
  1854.     if (theTE32KHandle && TE32KScrpHandle && TE32KScrpLength > 0L)
  1855.     {
  1856.         if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1857.             TE32KDelete( theTE32KHandle);
  1858.         
  1859.         HLock(TE32KScrpHandle);
  1860.         
  1861.         TE32KInsert(*TE32KScrpHandle,TE32KScrpLength,theTE32KHandle);
  1862.         
  1863.         HUnlock(TE32KScrpHandle);
  1864.     }
  1865. }
  1866.  
  1867.  
  1868.  
  1869.  
  1870. Handle    TE32KScrapHandle()
  1871. {
  1872.     return(TE32KScrpHandle);
  1873. }
  1874.  
  1875.  
  1876. long    TE32KGetScrapLen()
  1877. {
  1878.     return(TE32KScrpLength);
  1879. }
  1880.  
  1881.  
  1882. void    TE32KSetScrapLen(long newLength)
  1883. {
  1884.     TE32KScrpLength = newLength;
  1885. }
  1886.  
  1887.  
  1888.  
  1889.  
  1890. void    TE32KSelView(TE32KHandle theTE32KHandle)
  1891. {
  1892. register long    deltaV,deltaH,screenLines,lineHeight,viewTop,viewBot,selPtV,ascent;
  1893.  
  1894.     if (theTE32KHandle && (**theTE32KHandle).active)
  1895.     {
  1896.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd)
  1897.         {
  1898.             selPtV = (**theTE32KHandle).selPoint.v;
  1899.             viewTop = (**theTE32KHandle).viewRect.top;
  1900.             viewBot = (**theTE32KHandle).viewRect.bottom;
  1901.             lineHeight = (**theTE32KHandle).lineHeight;
  1902.             ascent = (**theTE32KHandle).fontAscent;
  1903.             
  1904.             deltaV = viewTop - (**theTE32KHandle).destRect.top;
  1905.             deltaV = deltaV - (deltaV/lineHeight)*lineHeight;
  1906.             
  1907.             if (selPtV - ascent < viewTop)
  1908.             {
  1909.                 deltaV += viewTop - (selPtV - ascent);
  1910.             }
  1911.             
  1912.             else if (selPtV > viewBot)
  1913.             {
  1914.                 screenLines = (viewBot - viewTop) / lineHeight;
  1915.                 
  1916.                 deltaV -= (selPtV - ascent + lineHeight) - (viewTop + screenLines * lineHeight);
  1917.             }
  1918.             
  1919.             
  1920.             if ((**theTE32KHandle).selPoint.h <= (**theTE32KHandle).viewRect.left)
  1921.             {
  1922.                 deltaH = (**theTE32KHandle).viewRect.left - (**theTE32KHandle).selPoint.h;
  1923.                 deltaH = (2L + deltaH/(**theTE32KHandle).lineHeight) * (**theTE32KHandle).lineHeight;
  1924.                 
  1925.                 if ((**theTE32KHandle).destRect.left + deltaH > (**theTE32KHandle).viewRect.left)
  1926.                     deltaH = (**theTE32KHandle).viewRect.left - (**theTE32KHandle).destRect.left;
  1927.             }
  1928.             
  1929.             else if ((**theTE32KHandle).selPoint.h > (**theTE32KHandle).viewRect.right)
  1930.             {
  1931.                 deltaH = (**theTE32KHandle).selPoint.h - (**theTE32KHandle).viewRect.right;
  1932.                 deltaH = -(2L + deltaH/(**theTE32KHandle).lineHeight) * (**theTE32KHandle).lineHeight;
  1933.             }
  1934.             
  1935.             else
  1936.                 deltaH = 0L;
  1937.             
  1938.             if (deltaV || deltaH)
  1939.                 TE32KScroll(deltaH,deltaV,theTE32KHandle);
  1940.         }
  1941.     }
  1942. }
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948. static    DoDeleteKey(TE32KHandle theTE32KHandle)
  1949. {
  1950. Rect            tempRect;
  1951. RgnHandle        updateRgn;
  1952. int                chWidth;
  1953. long            firstLine;
  1954. register int    *theCharWidths;
  1955. register long    i,*lineStarts,selIndex,*otherLine;
  1956. LongRect        updateRect;
  1957. LongPoint        selPt;
  1958. unsigned char    ch,prevChar;
  1959. GrafPtr            oldPort;
  1960.  
  1961.     if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  1962.         TE32KDelete(theTE32KHandle);
  1963.     
  1964.     else if ((**theTE32KHandle).selStart > 0L)
  1965.     {
  1966.         ch = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  1967.         
  1968.         if ((**theTE32KHandle).selStart >= 2L)
  1969.             prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 2L];
  1970.         else
  1971.             prevChar = '\0';
  1972.         
  1973.         firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  1974.         
  1975.         HLock((**theTE32KHandle).hText);
  1976.         BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart - 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selEnd);
  1977.         HUnlock((**theTE32KHandle).hText);
  1978.         
  1979.         (**theTE32KHandle).teLength--;
  1980.         (**theTE32KHandle).selStart--;
  1981.         (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  1982.         
  1983.         if ((ch == '\r' || ch == '\n') && ((**theTE32KHandle).crOnly || (**theTE32KHandle).teLength == (**theTE32KHandle).selStart || prevChar == '\r' || prevChar == '\n'))
  1984.         {
  1985.             lineStarts = &((**theTE32KHandle).lineStarts[firstLine]);
  1986.             otherLine = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  1987.             
  1988.             i = (**theTE32KHandle).nLines - firstLine;
  1989.             
  1990.             while (i--)
  1991.             {
  1992.                 selIndex = *(otherLine++);
  1993.                 *(lineStarts++) = --selIndex;
  1994.             }
  1995.             
  1996.             (**theTE32KHandle).nLines--;
  1997.             
  1998.             if (firstLine > 0L)
  1999.                 firstLine--;
  2000.             
  2001.             updateRect = (**theTE32KHandle).viewRect;
  2002.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2003.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  2004.             TE32KUpdate(&updateRect,theTE32KHandle);
  2005.             
  2006.             LongRectToRect(&updateRect,&tempRect);
  2007.             tempRect.top = tempRect.bottom;
  2008.             
  2009.             if ((**theTE32KHandle).viewRect.bottom < -32768L)
  2010.                 tempRect.bottom = -32768;
  2011.             else if ((**theTE32KHandle).viewRect.bottom > 32767L)
  2012.                 tempRect.bottom = 32767;
  2013.             else
  2014.                 tempRect.bottom = (int) (**theTE32KHandle).viewRect.bottom;
  2015.             
  2016.             
  2017.             GetPort(&oldPort);
  2018.             SetPort((**theTE32KHandle).inPort);
  2019.         
  2020.             updateRgn = NewRgn();
  2021.             ScrollRect(&tempRect,0,-(**theTE32KHandle).lineHeight,updateRgn);
  2022.             tempRect = (**updateRgn).rgnBBox;
  2023.             DisposeRgn(updateRgn);
  2024.             
  2025.             SetPort(oldPort);
  2026.             
  2027.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2028.             (**theTE32KHandle).selPoint = selPt;
  2029.             
  2030.             RectToLongRect(&tempRect,&updateRect);
  2031.             
  2032.             if ((**theTE32KHandle).caretState)
  2033.                 xorCaret(theTE32KHandle);
  2034.         
  2035.             TE32KUpdate(&updateRect,theTE32KHandle);
  2036.         }
  2037.         
  2038.         else
  2039.         {
  2040.             lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2041.             i = (**theTE32KHandle).nLines - firstLine;
  2042.             
  2043.             if (ch == '\r' || ch == '\n')
  2044.                 i++;
  2045.             
  2046.             
  2047.             while (i--)
  2048.                 (*(lineStarts--))--;
  2049.             
  2050.             theCharWidths = (**theTE32KHandle).theCharWidths;
  2051.             
  2052.             if (ch == TAB)
  2053.                 chWidth = (**theTE32KHandle).tabWidth;
  2054.             else
  2055.                 chWidth = theCharWidths[ch];
  2056.             
  2057.             if (ch == '\r' || ch == '\n')
  2058.             {
  2059.                 firstLine--;
  2060.                 
  2061.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2062.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  2063.                 updateRect.left = (**theTE32KHandle).viewRect.left;
  2064.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  2065.  
  2066.             }
  2067.             
  2068.             else
  2069.             {
  2070.                 updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2071.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  2072.                 updateRect.left = (**theTE32KHandle).selPoint.h - chWidth;
  2073.                 updateRect.right = (**theTE32KHandle).viewRect.right;
  2074.             }
  2075.             
  2076.             if ((**theTE32KHandle).caretState)
  2077.                 xorCaret(theTE32KHandle);
  2078.             
  2079.             if ((**theTE32KHandle).crOnly)
  2080.                 TE32KUpdate(&updateRect,theTE32KHandle);
  2081.             
  2082.             else
  2083.             {
  2084.                 updateLine(firstLine,theTE32KHandle,TRUE,&updateRect);
  2085.             }
  2086.         }
  2087.     }
  2088. }
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094. static    DoArrowKeys(unsigned char ch,TE32KHandle theTE32KHandle)
  2095. {
  2096. LongPoint        selPt,tempPt1,tempPt2;
  2097. long            firstLine,selIndex;
  2098. unsigned char    currentChar;
  2099.  
  2100.     if ((**theTE32KHandle).caretState)
  2101.         xorCaret(theTE32KHandle);
  2102.     
  2103.     if (!shiftKeyDown())
  2104.     {
  2105.         if (ch==LEFTARROW || ch==RIGHTARROW)
  2106.         {
  2107.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  2108.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2109.             
  2110.             if (ch==LEFTARROW && (**theTE32KHandle).selStart > 0L)
  2111.             {
  2112.                 currentChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).selStart - 1L];
  2113.                 
  2114.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2115.                 
  2116.                 (**theTE32KHandle).clikStuff = FALSE;
  2117.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2118.                 
  2119.                 if (!(**theTE32KHandle).crOnly && firstLine > 0 && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine] && currentChar != '\r'  && currentChar != '\n' && (**theTE32KHandle).selPoint.h == selPt.h && (**theTE32KHandle).selPoint.v == selPt.v)
  2120.                 {
  2121.                     (**theTE32KHandle).clikStuff = TRUE;
  2122.                     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2123.                     
  2124.                     (**theTE32KHandle).selPoint = selPt;
  2125.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2126.                     
  2127.                     return;
  2128.                 }
  2129.                 
  2130.                 else
  2131.                 {
  2132.                     (**theTE32KHandle).selStart--;
  2133.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2134.                 }
  2135.             }
  2136.             else if (ch==RIGHTARROW && (**theTE32KHandle).selEnd < (**theTE32KHandle).teLength)
  2137.             {
  2138.                 currentChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).selEnd];
  2139.                 
  2140.                 firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2141.                 
  2142.                 (**theTE32KHandle).clikStuff = TRUE;
  2143.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2144.                 
  2145.                 if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selEnd == (**theTE32KHandle).lineStarts[firstLine] && (**theTE32KHandle).selPoint.h == selPt.h && (**theTE32KHandle).selPoint.v == selPt.v)
  2146.                 {
  2147.                     (**theTE32KHandle).clikStuff = FALSE;
  2148.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2149.                     
  2150.                     (**theTE32KHandle).selPoint = selPt;
  2151.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2152.                     
  2153.                     return;
  2154.                 }
  2155.                 
  2156.                 else if (!(**theTE32KHandle).crOnly && firstLine < (**theTE32KHandle).nLines - 1L && (**theTE32KHandle).selStart + 1L == (**theTE32KHandle).lineStarts[firstLine + 1L] && currentChar != '\r' && currentChar != '\n')
  2157.                 {
  2158.                     (**theTE32KHandle).selEnd++;
  2159.                     
  2160.                     (**theTE32KHandle).clikStuff = TRUE;
  2161.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2162.                     
  2163.                     (**theTE32KHandle).selPoint = selPt;
  2164.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2165.                     
  2166.                     return;
  2167.                 }
  2168.                 
  2169.                 else
  2170.                 {
  2171.                     (**theTE32KHandle).selEnd++;
  2172.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2173.                 }
  2174.             }
  2175.             
  2176.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2177.         }
  2178.         
  2179.         else if (ch==UPARROW || ch==DOWNARROW)
  2180.         {
  2181.             if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd)
  2182.             {
  2183.                 invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2184.                 
  2185.                 if (ch == DOWNARROW)
  2186.                     TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2187.                 else
  2188.                     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2189.                 
  2190.                 (**theTE32KHandle).selPoint = selPt;
  2191.             }
  2192.                 
  2193.                 
  2194.             if (ch==UPARROW)
  2195.             {
  2196.                 selPt = (**theTE32KHandle).selPoint;
  2197.                 
  2198.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2199.                 
  2200.                 if (firstLine > 0L)
  2201.                 {
  2202.                     selPt.v -= (**theTE32KHandle).lineHeight;
  2203.                     (**theTE32KHandle).selStart = TE32KGetOffset(&selPt,theTE32KHandle);
  2204.                     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2205.                     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2206.                     
  2207.                     if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2208.                     {
  2209.                         (**theTE32KHandle).clikStuff = FALSE;
  2210.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt1,theTE32KHandle);
  2211.                         
  2212.                         (**theTE32KHandle).clikStuff = TRUE;
  2213.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt2,theTE32KHandle);
  2214.                         
  2215.                         if ((selPt.h - tempPt1.h)*(selPt.h - tempPt1.h) + (selPt.v - tempPt1.v)*(selPt.v - tempPt1.v) <
  2216.                             (selPt.h - tempPt2.h)*(selPt.h - tempPt2.h) + (selPt.v - tempPt2.v)*(selPt.v - tempPt2.v))
  2217.                                 (**theTE32KHandle).selPoint = tempPt1;
  2218.                         else
  2219.                                 (**theTE32KHandle).selPoint = tempPt2;
  2220.                         
  2221.                         return;
  2222.                     }
  2223.                     
  2224.                     else
  2225.                     {
  2226.                         (**theTE32KHandle).clikStuff = FALSE;
  2227.                         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2228.                         (**theTE32KHandle).selPoint = selPt;
  2229.                     }
  2230.                 }
  2231.             }
  2232.             
  2233.             else if (ch == DOWNARROW)
  2234.             {
  2235.                 selPt = (**theTE32KHandle).selPoint;
  2236.                 
  2237.                 firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2238.                 
  2239.                 if (firstLine < (**theTE32KHandle).nLines)
  2240.                 {
  2241.                     selPt.v += (**theTE32KHandle).lineHeight;
  2242.                     (**theTE32KHandle).selEnd = TE32KGetOffset(&selPt,theTE32KHandle);
  2243.                     (**theTE32KHandle).selStart = (**theTE32KHandle).selEnd;
  2244.                     firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2245.                     
  2246.                     if (!(**theTE32KHandle).crOnly && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2247.                     {
  2248.                         (**theTE32KHandle).clikStuff = FALSE;
  2249.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt1,theTE32KHandle);
  2250.                         
  2251.                         (**theTE32KHandle).clikStuff = TRUE;
  2252.                         TE32KGetPoint((**theTE32KHandle).selStart,&tempPt2,theTE32KHandle);
  2253.                         
  2254.                         if ((selPt.h - tempPt1.h)*(selPt.h - tempPt1.h) + (selPt.v - tempPt1.v)*(selPt.v - tempPt1.v) <
  2255.                             (selPt.h - tempPt2.h)*(selPt.h - tempPt2.h) + (selPt.v - tempPt2.v)*(selPt.v - tempPt2.v))
  2256.                                 (**theTE32KHandle).selPoint = tempPt1;
  2257.                         else
  2258.                                 (**theTE32KHandle).selPoint = tempPt2;
  2259.                         
  2260.                         return;
  2261.                     }
  2262.                     
  2263.                     else
  2264.                     {
  2265.                         (**theTE32KHandle).clikStuff = FALSE;
  2266.                         TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2267.                         (**theTE32KHandle).selPoint = selPt;
  2268.                     }
  2269.                 }
  2270.             }
  2271.             
  2272.             (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2273.             
  2274.             invertSelRange((**theTE32KHandle).selStart,(**theTE32KHandle).selEnd,theTE32KHandle);
  2275.         }
  2276.     }
  2277.     
  2278.     else
  2279.     {
  2280.         if (ch==LEFTARROW)
  2281.         {
  2282.             if ((**theTE32KHandle).selStart > 0L)
  2283.             {
  2284.                 invertSelRange((**theTE32KHandle).selStart - 1L,(**theTE32KHandle).selStart,theTE32KHandle);
  2285.                 (**theTE32KHandle).selStart--;
  2286.             }
  2287.         }
  2288.         
  2289.         else if (ch==RIGHTARROW)
  2290.         {
  2291.             if ((**theTE32KHandle).selEnd < (**theTE32KHandle).teLength)
  2292.             {
  2293.                 invertSelRange((**theTE32KHandle).selEnd,(**theTE32KHandle).selEnd + 1L,theTE32KHandle);
  2294.                 (**theTE32KHandle).selEnd++;
  2295.             }
  2296.         }
  2297.         
  2298.         else if (ch==UPARROW)
  2299.         {
  2300.             firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2301.             
  2302.             if (firstLine > 0L)
  2303.             {
  2304.                 TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2305.                 selPt.v -= (**theTE32KHandle).lineHeight;
  2306.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  2307.                 
  2308.                 invertSelRange(selIndex,(**theTE32KHandle).selStart,theTE32KHandle);
  2309.                 
  2310.                 (**theTE32KHandle).selStart = selIndex;
  2311.             }
  2312.         }
  2313.         
  2314.         else if (ch==DOWNARROW)
  2315.         {
  2316.             firstLine = indexToLine((**theTE32KHandle).selEnd,theTE32KHandle);
  2317.             
  2318.             if (firstLine < (**theTE32KHandle).nLines - 1L)
  2319.             {
  2320.                 TE32KGetPoint((**theTE32KHandle).selEnd,&selPt,theTE32KHandle);
  2321.                 selPt.v += (**theTE32KHandle).lineHeight;
  2322.                 selIndex = TE32KGetOffset(&selPt,theTE32KHandle);
  2323.                 
  2324.                 invertSelRange((**theTE32KHandle).selEnd,selIndex,theTE32KHandle);
  2325.                 
  2326.                 (**theTE32KHandle).selEnd = selIndex;
  2327.             }
  2328.         }
  2329.     }
  2330. }
  2331.  
  2332.  
  2333.  
  2334.  
  2335.  
  2336. static    DoNormalChar(unsigned char ch,TE32KHandle theTE32KHandle)
  2337. {
  2338. Rect            tempRect;
  2339. RgnHandle        updateRgn;
  2340. int                chWidth,destLeftSide;
  2341. register int    *theCharWidths;
  2342. long            teLength,firstLine;
  2343. register long    i,*lineStarts,delta;
  2344. LongPoint        selPt;
  2345. unsigned char    prevChar;
  2346. GrafPtr            oldPort;
  2347. int                oldFont,oldFace,oldSize,oldMode;
  2348.     
  2349.     teLength = (**theTE32KHandle).teLength + 1L;
  2350.     
  2351.     if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  2352.     {
  2353.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2354.         
  2355.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2356.             return;
  2357.     }
  2358.     
  2359.     if ((**theTE32KHandle).caretState)
  2360.         xorCaret(theTE32KHandle);
  2361.     
  2362.     selPt = (**theTE32KHandle).selPoint;
  2363.     
  2364.     selPt.h--;
  2365.     
  2366.     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2367.     if ((**theTE32KHandle).selStart > 0L)
  2368.         prevChar = ((unsigned char *) *(**theTE32KHandle).hText)[(**theTE32KHandle).lineStarts[firstLine] - 1L];
  2369.     else
  2370.         prevChar = '\r';
  2371.     
  2372.     if ((**theTE32KHandle).crOnly || prevChar =='\r'  || prevChar =='\n' || !(ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine]))
  2373.     {
  2374.         if (selPt.h < -32768L)
  2375.             tempRect.left = -32768;
  2376.         else if (selPt.h > 32767L)
  2377.             tempRect.left = 32767;
  2378.         else
  2379.             tempRect.left = (int) selPt.h;
  2380.         
  2381.         if ((**theTE32KHandle).viewRect.right < -32768L)
  2382.             tempRect.right = -32768;
  2383.         else if ((**theTE32KHandle).viewRect.right > 32767L)
  2384.             tempRect.right = 32767;
  2385.         else
  2386.             tempRect.right = (int) (**theTE32KHandle).viewRect.right;
  2387.         
  2388.         selPt.v -= (**theTE32KHandle).fontAscent;
  2389.         
  2390.         if (selPt.v < -32768L)
  2391.             tempRect.top = -32768;
  2392.         else if (selPt.v > 32767L)
  2393.             tempRect.top = 32767;
  2394.         else
  2395.             tempRect.top = (int) selPt.v;
  2396.         
  2397.         tempRect.bottom = tempRect.top + (**theTE32KHandle).lineHeight;
  2398.         
  2399.         GetPort(&oldPort);
  2400.         SetPort((**theTE32KHandle).inPort);
  2401.         
  2402.         oldFont = ((**theTE32KHandle).inPort)->txFont;
  2403.         oldFace = ((**theTE32KHandle).inPort)->txFace;
  2404.         oldSize = ((**theTE32KHandle).inPort)->txSize;
  2405.         oldMode = ((**theTE32KHandle).inPort)->txMode;
  2406.         
  2407.         TextFont((**theTE32KHandle).txFont);
  2408.         TextFace((**theTE32KHandle).txFace);
  2409.         TextSize((**theTE32KHandle).txSize);
  2410.         TextMode((**theTE32KHandle).txMode);
  2411.         
  2412.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2413.         
  2414.         if (ch == TAB)
  2415.         {
  2416.             destLeftSide = (**theTE32KHandle).destRect.left + 1L;
  2417.             delta = (**theTE32KHandle).tabWidth;
  2418.             chWidth = (destLeftSide + ((tempRect.left + 1 - destLeftSide + delta)/delta)*delta) - (tempRect.left + 1);
  2419.         }
  2420.         else
  2421.             chWidth = theCharWidths[ch];
  2422.         
  2423.         if (tempRect.left < tempRect.right)
  2424.         {    
  2425.             updateRgn = NewRgn();
  2426.             ScrollRect(&tempRect,chWidth,0,updateRgn);
  2427.             
  2428.             if (tempRect.left+1 + chWidth > tempRect.right)
  2429.                 ClipRect(&tempRect);
  2430.             
  2431.             MoveTo(tempRect.left+1,tempRect.top + (**theTE32KHandle).fontAscent);
  2432.             if (ch != TAB)
  2433.                 DrawChar(ch);
  2434.             
  2435.             if (tempRect.left+1 + chWidth > tempRect.right)
  2436.             {
  2437.                 tempRect.left = -32768;
  2438.                 tempRect.top = -32768;
  2439.                 tempRect.right = 32767;
  2440.                 tempRect.bottom = 32767;
  2441.                 ClipRect(&tempRect);
  2442.             }
  2443.             
  2444.             DisposeRgn(updateRgn);
  2445.         }
  2446.         
  2447.         TextFont(oldFont);
  2448.         TextFace(oldFace);
  2449.         TextSize(oldSize);
  2450.         TextMode(oldMode);
  2451.         
  2452.         SetPort(oldPort);
  2453.     }
  2454.     
  2455.     HLock((**theTE32KHandle).hText);
  2456.     BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2457.     HUnlock((**theTE32KHandle).hText);
  2458.     
  2459.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = ch;
  2460.     
  2461.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2462.     i = (**theTE32KHandle).nLines - firstLine;
  2463.     
  2464.     if (!(**theTE32KHandle).crOnly && prevChar != '\r'  && prevChar != '\n' && ch == ' ' && (**theTE32KHandle).selStart == (**theTE32KHandle).lineStarts[firstLine])
  2465.         i++;
  2466.     
  2467.     while (i--)
  2468.         (*(lineStarts--))++;
  2469.     
  2470.     
  2471.     (**theTE32KHandle).teLength++;
  2472.     (**theTE32KHandle).selStart++;
  2473.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2474.     (**theTE32KHandle).selPoint.h += (long) chWidth;
  2475.     
  2476.     if (!(**theTE32KHandle).crOnly)
  2477.         updateLine(firstLine,theTE32KHandle,FALSE,0L);
  2478.     
  2479.     xorCaret(theTE32KHandle);
  2480. }
  2481.  
  2482.  
  2483.  
  2484.  
  2485.  
  2486. static    DoReturnChar(TE32KHandle theTE32KHandle)
  2487. {
  2488. Rect            tempRect;
  2489. RgnHandle        updateRgn;
  2490. long            teLength,firstLine,lastLine,deltaLines,numAffected,tempFirstLine;
  2491. register long    i,*lineStarts,selIndex,*otherLine;
  2492. LongPoint        selPt;
  2493. LongRect        updateRect;
  2494. unsigned char    prevChar,doWrap;
  2495. GrafPtr            oldPort;
  2496.  
  2497.     teLength = GetHandleSize((Handle) theTE32KHandle);
  2498.     lastLine  = (teLength - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2499.     
  2500.     if ((**theTE32KHandle).nLines + 1L >= lastLine)
  2501.     {
  2502.         teLength = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + 1L + EXTRALINESTARTS);
  2503.         
  2504.         SetHandleSize((Handle) theTE32KHandle,teLength);
  2505.         
  2506.         if (MemError()  || GetHandleSize(theTE32KHandle) < teLength)
  2507.             return;
  2508.     }
  2509.     
  2510.     
  2511.     teLength = (**theTE32KHandle).teLength + 1L;
  2512.     
  2513.     if (GetHandleSize((**theTE32KHandle).hText) < teLength)
  2514.     {
  2515.         SetHandleSize((**theTE32KHandle).hText,teLength + EXTRATEXTBUFF);
  2516.         
  2517.         if (MemError() || GetHandleSize((**theTE32KHandle).hText) < teLength)
  2518.             return;
  2519.     }
  2520.     
  2521.     
  2522.     
  2523.     if ((**theTE32KHandle).selStart > 0L)
  2524.         prevChar = ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart - 1L];
  2525.     else
  2526.         prevChar = '\r';
  2527.     
  2528.     if ((**theTE32KHandle).caretState)
  2529.         xorCaret(theTE32KHandle);
  2530.     
  2531.     HLock((**theTE32KHandle).hText);
  2532.     BlockMove(*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart,*((**theTE32KHandle).hText) + (**theTE32KHandle).selStart + 1L,(**theTE32KHandle).teLength - (**theTE32KHandle).selStart);
  2533.     HUnlock((**theTE32KHandle).hText);
  2534.     
  2535.     ((unsigned char *) *((**theTE32KHandle).hText))[(**theTE32KHandle).selStart] = RETURN;
  2536.     
  2537.     firstLine = indexToLine((**theTE32KHandle).selStart,theTE32KHandle);
  2538.     
  2539.     lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  2540.     otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + 1L]);
  2541.     i = (**theTE32KHandle).nLines - firstLine;
  2542.         
  2543.     while (i--)
  2544.     {
  2545.         selIndex = *(lineStarts--);
  2546.         *(otherLine--) = ++selIndex;
  2547.     
  2548.     }
  2549.     
  2550.     (**theTE32KHandle).lineStarts[firstLine + 1L] = (**theTE32KHandle).selStart + 1L;
  2551.     
  2552.     (**theTE32KHandle).nLines++;
  2553.     (**theTE32KHandle).teLength++;
  2554.     (**theTE32KHandle).selStart++;
  2555.     (**theTE32KHandle).selEnd = (**theTE32KHandle).selStart;
  2556.     
  2557.     LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2558.     
  2559.     selPt = (**theTE32KHandle).selPoint;
  2560.     selPt.v -= (**theTE32KHandle).fontAscent;
  2561.     selPt.v += (**theTE32KHandle).lineHeight;
  2562.     
  2563.     if (selPt.v < -32768L)
  2564.         tempRect.top = -32768;
  2565.     else if (selPt.v > 32767L)
  2566.         tempRect.top = 32767;
  2567.     else
  2568.         tempRect.top = (int) selPt.v;
  2569.     
  2570.     GetPort(&oldPort);
  2571.     SetPort((**theTE32KHandle).inPort);
  2572.             
  2573.     updateRgn = NewRgn();
  2574.     ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight,updateRgn);
  2575.     DisposeRgn(updateRgn);
  2576.     
  2577.     SetPort(oldPort);
  2578.     
  2579.     if (!(**theTE32KHandle).crOnly)
  2580.     {
  2581.         doWrap = FALSE;
  2582.         tempFirstLine = firstLine;
  2583.         
  2584.         if (tempFirstLine > 0L && LineEndIndex(tempFirstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine])
  2585.         {
  2586.             doWrap = TRUE;
  2587.             tempFirstLine--;
  2588.         }
  2589.         
  2590.         else if (LineEndIndex(tempFirstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[tempFirstLine + 1L])
  2591.             doWrap = TRUE;
  2592.         
  2593.         
  2594.         if (doWrap)
  2595.         {
  2596.             CalParagraph(tempFirstLine,theTE32KHandle,&deltaLines,&numAffected);
  2597.             
  2598.             if (deltaLines == 0L)
  2599.             {
  2600.                 updateRect = (**theTE32KHandle).viewRect;
  2601.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2602.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2603.             }
  2604.             
  2605.             else if (deltaLines > 0L)
  2606.             {
  2607.                 firstLine += deltaLines;
  2608.                 
  2609.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2610.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2611.                 
  2612.                 GetPort(&oldPort);
  2613.                 SetPort((**theTE32KHandle).inPort);
  2614.                 
  2615.                 updateRgn = NewRgn();
  2616.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2617.                 DisposeRgn(updateRgn);
  2618.                 
  2619.                 SetPort(oldPort);
  2620.                 
  2621.                 updateRect = (**theTE32KHandle).viewRect;
  2622.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2623.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2624.             }
  2625.             
  2626.             else
  2627.             {
  2628.                 firstLine += deltaLines;
  2629.                 
  2630.                 LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2631.                 tempRect.top = (**theTE32KHandle).destRect.top + (tempFirstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2632.                 
  2633.                 GetPort(&oldPort);
  2634.                 SetPort((**theTE32KHandle).inPort);
  2635.     
  2636.                 updateRgn = NewRgn();
  2637.                 ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2638.                 DisposeRgn(updateRgn);
  2639.                 
  2640.                 SetPort(oldPort);
  2641.                 
  2642.                 updateRect = (**theTE32KHandle).viewRect;
  2643.                 updateRect.top = (**theTE32KHandle).destRect.top + tempFirstLine * (**theTE32KHandle).lineHeight;
  2644.                 updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  2645.             }
  2646.             
  2647.             TE32KUpdate(&updateRect,theTE32KHandle);
  2648.         }
  2649.         
  2650.         
  2651.         firstLine++;
  2652.         
  2653.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  2654.         
  2655.         if (deltaLines > 0L)
  2656.         {
  2657.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2658.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  2659.             
  2660.             GetPort(&oldPort);
  2661.             SetPort((**theTE32KHandle).inPort);
  2662.     
  2663.             updateRgn = NewRgn();
  2664.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2665.             DisposeRgn(updateRgn);
  2666.             
  2667.             SetPort(oldPort);
  2668.         }
  2669.         
  2670.         else if (deltaLines < 0L)
  2671.         {
  2672.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  2673.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  2674.             
  2675.             GetPort(&oldPort);
  2676.             SetPort((**theTE32KHandle).inPort);
  2677.     
  2678.             updateRgn = NewRgn();
  2679.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  2680.             DisposeRgn(updateRgn);
  2681.             
  2682.             SetPort(oldPort);
  2683.         }
  2684.         
  2685.         updateRect = (**theTE32KHandle).viewRect;
  2686.         updateRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L) * (**theTE32KHandle).lineHeight;
  2687.         updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * (numAffected + 1L);
  2688.         
  2689.         if ((**theTE32KHandle).caretState)
  2690.             xorCaret(theTE32KHandle);
  2691.         
  2692.         TE32KUpdate(&updateRect,theTE32KHandle);
  2693.         
  2694.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2695.         (**theTE32KHandle).selPoint = selPt;
  2696.     }
  2697.     
  2698.     else
  2699.     {
  2700.         updateRect = (**theTE32KHandle).viewRect;
  2701.         TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2702.         (**theTE32KHandle).selPoint = selPt;
  2703.         
  2704.         if ((**theTE32KHandle).nLines - firstLine >= 2L && (**theTE32KHandle).lineStarts[firstLine+1L]+1L < (**theTE32KHandle).lineStarts[firstLine + 2L])
  2705.         {
  2706.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  2707.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight + (**theTE32KHandle).lineHeight;
  2708.             TE32KUpdate(&updateRect,theTE32KHandle);
  2709.         }
  2710.         else
  2711.         {
  2712.             TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  2713.             (**theTE32KHandle).selPoint = selPt;
  2714.             xorCaret(theTE32KHandle);
  2715.         }
  2716.     }
  2717. }
  2718.  
  2719.  
  2720.  
  2721. static    OverTypeSelection(unsigned char ch,TE32KHandle theTE32KHandle)
  2722. {
  2723.     TE32KDelete(theTE32KHandle);
  2724.     
  2725.     if (ch==RETURN)
  2726.         DoReturnChar(theTE32KHandle);
  2727.     
  2728.     else if (ch==TAB || ch >= (unsigned char) 0x20)
  2729.         DoNormalChar(ch,theTE32KHandle);
  2730. }
  2731.  
  2732.  
  2733.  
  2734.  
  2735.  
  2736.  
  2737.  
  2738.  
  2739.  
  2740. void    TE32KKey(unsigned char ch,TE32KHandle theTE32KHandle)
  2741. {
  2742.     if (theTE32KHandle && (**theTE32KHandle).active)
  2743.     {
  2744.         ObscureCursor();
  2745.         
  2746.         if (ch == ENTER)
  2747.             ch = RETURN;
  2748.             
  2749.         
  2750.         if (ch == DELETE)
  2751.             DoDeleteKey(theTE32KHandle);
  2752.         
  2753.         else if (ch==LEFTARROW || ch==RIGHTARROW || ch==UPARROW || ch==DOWNARROW)
  2754.             DoArrowKeys(ch,theTE32KHandle);
  2755.         
  2756.         else if ((**theTE32KHandle).selStart < (**theTE32KHandle).selEnd && (ch >= 0x20 || ch==TAB || ch==RETURN))
  2757.             OverTypeSelection(ch,theTE32KHandle);
  2758.         
  2759.         else if (ch==TAB || ch >= (unsigned char) 0x20)
  2760.             DoNormalChar(ch,theTE32KHandle);
  2761.         
  2762.         else if (ch==RETURN)
  2763.             DoReturnChar(theTE32KHandle);
  2764.         
  2765.         if ((**theTE32KHandle).selStart == (**theTE32KHandle).selEnd && !(**theTE32KHandle).caretState)
  2766.             xorCaret(theTE32KHandle);
  2767.     }
  2768. }
  2769.  
  2770.  
  2771.  
  2772.  
  2773.  
  2774. static long paraLines(long firstLine, TE32KHandle theTE32KHandle)
  2775. {
  2776. long                    lastLine,nLines;
  2777. register unsigned char    *charBase;
  2778. register long            *lineStarts;
  2779.  
  2780.     if ((**theTE32KHandle).crOnly)
  2781.         return(1L);
  2782.     
  2783.     lastLine = firstLine + 1L;
  2784.     nLines = (**theTE32KHandle).nLines;
  2785.     charBase = (unsigned char    *) *((**theTE32KHandle).hText);
  2786.     lineStarts = &((**theTE32KHandle).lineStarts[lastLine]);
  2787.     
  2788.     while (lastLine < nLines && charBase[*lineStarts - 1L] != '\r' && charBase[*lineStarts - 1L] != '\n')
  2789.     {
  2790.         lastLine++;
  2791.         lineStarts++;
  2792.     }
  2793.     
  2794.     return(lastLine - firstLine);
  2795. }
  2796.  
  2797.  
  2798.  
  2799.  
  2800.  
  2801. static long    LineEndIndex(long firstLine,TE32KHandle theTE32KHandle)
  2802. {
  2803. register unsigned char    *charPtr;
  2804. register long            charCount;
  2805. register int            *theCharWidths,crOnly,maxLineWidth,lineWidth;
  2806. register unsigned char    ch;
  2807. unsigned char            *charBase;
  2808. Point                    cursorPt;
  2809. int                        rightSide,destLeftSide,tabWidth;
  2810. int                        lineStatus;
  2811. unsigned char            *oldCharPtr;
  2812. long                    maxRewind;
  2813.     
  2814.     if ((**theTE32KHandle).crOnly)
  2815.         return((**theTE32KHandle).lineStarts[firstLine + 1L]);
  2816.     
  2817.     maxLineWidth = (**theTE32KHandle).maxLineWidth;
  2818.     crOnly = (**theTE32KHandle).crOnly;
  2819.     
  2820.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2821.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2822.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2823.     
  2824.     if (charCount > (**theTE32KHandle).teLength)
  2825.         charCount = (**theTE32KHandle).teLength;
  2826.     
  2827.     lineStatus = 0;
  2828.     lineWidth = 0;
  2829.     
  2830.     if (charCount)
  2831.     {
  2832.         rightSide = (int) ((**theTE32KHandle).destRect.right);
  2833.         destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  2834.         cursorPt.h = destLeftSide;
  2835.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2836.         
  2837.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2838.         
  2839.         ch = ' ';
  2840.         
  2841.         while (charCount-- && ch != '\r' && ch != '\n')
  2842.         {
  2843.             ch = *charPtr++;
  2844.             lineWidth++;
  2845.             
  2846.             if (ch == TAB)
  2847.                 cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2848.             else if (ch != '\r' && ch != '\n')
  2849.                 cursorPt.h += theCharWidths[ch];
  2850.             
  2851.             if ((cursorPt.h >= rightSide && ch != ' ') || (!crOnly && lineWidth > maxLineWidth))
  2852.             {
  2853.                 maxRewind = charPtr - charBase - (**theTE32KHandle).lineStarts[firstLine];
  2854.                 oldCharPtr = charPtr;
  2855.                 
  2856.                 charPtr--;
  2857.                 maxRewind--;
  2858.                 
  2859.                 while (*charPtr != ' ' && maxRewind > 0)
  2860.                 {
  2861.                     charPtr--;
  2862.                     maxRewind--;
  2863.                 }
  2864.                 
  2865.                 if (maxRewind <= 0)
  2866.                     charPtr = oldCharPtr;
  2867.                 
  2868.                 else
  2869.                     charPtr++;
  2870.                 
  2871.                 charCount = 0;
  2872.             }
  2873.         }
  2874.     }
  2875.     
  2876.     return(charPtr - charBase);
  2877. }
  2878.  
  2879.  
  2880.  
  2881. #define    NUMTEMPLINES    32
  2882.  
  2883. static void    CalParagraph(long firstLine,TE32KHandle theTE32KHandle,long *theDeltaLines,long *theNumAffected)
  2884.  
  2885. {
  2886. register unsigned char    *charPtr;
  2887. register int            *theCharWidths;
  2888. register long            charCount,*lineStarts,*otherLine,i;
  2889. register long            crOnly,lineWidth,maxLineWidth;
  2890. register unsigned char    ch;
  2891. register long            nLines;
  2892. long                    maxLineStarts,sizeTE32KHandle,oldCharCount;
  2893. unsigned char            *charBase;
  2894. Point                    cursorPt;
  2895. int                        rightSide,destLeftSide,tabWidth,maxRewind;
  2896. unsigned char            *oldCharPtr;
  2897. long                    tempLineStarts[NUMTEMPLINES],oldNumLines,deltaLines;
  2898.     
  2899.     if ((**theTE32KHandle).crOnly)
  2900.     {
  2901.         *theDeltaLines = 0L;
  2902.         *theNumAffected = 0L;
  2903.         return;
  2904.     }
  2905.     
  2906.     deltaLines = 0L;
  2907.     
  2908.     oldNumLines = paraLines(firstLine,theTE32KHandle);
  2909.     
  2910.     for (i=0;i<oldNumLines && i <NUMTEMPLINES;i++)
  2911.         tempLineStarts[i] = (**theTE32KHandle).lineStarts[firstLine + i];
  2912.         
  2913.     sizeTE32KHandle  = GetHandleSize((Handle) theTE32KHandle);
  2914.     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  2915.     
  2916.     nLines = 0;
  2917.     tempLineStarts[nLines] = (**theTE32KHandle).lineStarts[firstLine];
  2918.     
  2919.     crOnly = (**theTE32KHandle).crOnly;
  2920.     maxLineWidth = (**theTE32KHandle).maxLineWidth;
  2921.     lineWidth = 0;
  2922.     
  2923.     charBase = (unsigned char *) *((**theTE32KHandle).hText);
  2924.     charPtr = charBase + (**theTE32KHandle).lineStarts[firstLine];
  2925.     
  2926.     charCount = (**theTE32KHandle).teLength - (**theTE32KHandle).lineStarts[firstLine];
  2927.     ch = *charPtr;
  2928.     
  2929.     if (charCount > 0L)
  2930.     {
  2931.         rightSide = (int) ((**theTE32KHandle).destRect.right);
  2932.         destLeftSide = (int) ((**theTE32KHandle).destRect.left + 1L);
  2933.         cursorPt.h = destLeftSide;
  2934.         tabWidth = (long) (**theTE32KHandle).tabWidth;
  2935.         
  2936.         theCharWidths = (**theTE32KHandle).theCharWidths;
  2937.         
  2938.         ch = ' ';
  2939.         
  2940.         while (ch != '\r' && ch != '\n' && charCount--)
  2941.         {
  2942.             ch = *charPtr++;
  2943.             lineWidth++;
  2944.             
  2945.             if (ch != '\r' && ch != '\n')
  2946.             {
  2947.                 if (ch == TAB)
  2948.                     cursorPt.h = destLeftSide + ((cursorPt.h - destLeftSide + tabWidth)/tabWidth)*tabWidth;
  2949.                 else
  2950.                     cursorPt.h += theCharWidths[ch];
  2951.                 
  2952.                 if ((cursorPt.h >= rightSide && ch != ' ') || (!crOnly && lineWidth > maxLineWidth))
  2953.                 {
  2954.                     maxRewind = charPtr - charBase - tempLineStarts[nLines];
  2955.                     oldCharPtr = charPtr;
  2956.                     oldCharCount = charCount;
  2957.                     
  2958.                     charPtr--;
  2959.                     charCount++;
  2960.                     maxRewind--;
  2961.                     
  2962.                     while (*charPtr != ' ' && maxRewind > 0)
  2963.                     {
  2964.                         charPtr--;
  2965.                         charCount++;
  2966.                         maxRewind--;
  2967.                     }
  2968.                     
  2969.                     if (maxRewind <= 0)
  2970.                     {
  2971.                         charPtr = oldCharPtr;
  2972.                         charCount = oldCharCount;
  2973.                     }
  2974.                     
  2975.                     else
  2976.                     {
  2977.                         charPtr++;
  2978.                         charCount--;
  2979.                     }
  2980.                     
  2981.                     nLines++;
  2982.                     
  2983.                     if (nLines < NUMTEMPLINES)
  2984.                     {
  2985.                         if (tempLineStarts[nLines] == charPtr - charBase)
  2986.                         {
  2987.                             oldNumLines = nLines;
  2988.                             goto STOPWRAPPING;
  2989.                         }
  2990.                         else
  2991.                             tempLineStarts[nLines] = charPtr - charBase;
  2992.                     }
  2993.                     
  2994.                     else
  2995.                         goto STOPWRAPPING;
  2996.                     
  2997.                     cursorPt.h = destLeftSide;
  2998.                     lineWidth = 0;
  2999.                 }
  3000.             }
  3001.         }
  3002.         
  3003.         nLines++;
  3004.         
  3005.         if (nLines < NUMTEMPLINES)
  3006.             tempLineStarts[nLines] = charPtr - charBase;
  3007.  
  3008. STOPWRAPPING:
  3009.  
  3010.         deltaLines = nLines - oldNumLines;
  3011.  
  3012.         if (nLines >= NUMTEMPLINES)
  3013.         {
  3014.             TE32KCalText(theTE32KHandle);
  3015.             deltaLines = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  3016.         }
  3017.         
  3018.         else
  3019.         {
  3020.             if (deltaLines == 0L)
  3021.             {
  3022.                 for (i = 1;i <= nLines;i++)
  3023.                     (**theTE32KHandle).lineStarts[firstLine + i] = tempLineStarts[i];
  3024.             }
  3025.             
  3026.             else if (deltaLines < 0L)
  3027.             {
  3028.                 lineStarts = &((**theTE32KHandle).lineStarts[firstLine + 1L]);
  3029.                 
  3030.                 for (i = 1;i <= nLines;i++)
  3031.                     *(lineStarts++) = tempLineStarts[i];
  3032.                 
  3033.                 otherLine = &((**theTE32KHandle).lineStarts[firstLine + oldNumLines + 1L]);
  3034.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines + 1L;
  3035.                 
  3036.                 while (i--)
  3037.                     *(lineStarts++) = *(otherLine++);
  3038.                 
  3039.                 (**theTE32KHandle).nLines += deltaLines;
  3040.             }
  3041.             
  3042.             else
  3043.             {
  3044.                 if ((**theTE32KHandle).nLines + deltaLines >= maxLineStarts)
  3045.                 {
  3046.                     sizeTE32KHandle = (long) sizeof(TE32KRec) + (long) sizeof(long)*((**theTE32KHandle).nLines + deltaLines + EXTRALINESTARTS);
  3047.                     maxLineStarts = (sizeTE32KHandle - (long) sizeof(TE32KRec))/(long) sizeof(long) - 2;
  3048.                     
  3049.                     SetHandleSize((Handle) theTE32KHandle,sizeTE32KHandle);
  3050.                     
  3051.                     if (MemError())
  3052.                     {
  3053.                         nLines = (**theTE32KHandle).nLines;
  3054.                         deltaLines = (**theTE32KHandle).nLines;
  3055.                         goto EXITPOINT;
  3056.                     }
  3057.                 }
  3058.                 
  3059.                 lineStarts = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines]);
  3060.                 otherLine = &((**theTE32KHandle).lineStarts[(**theTE32KHandle).nLines + deltaLines]);
  3061.                 i = (**theTE32KHandle).nLines - firstLine - oldNumLines;
  3062.                 
  3063.                 while (i--)
  3064.                     *(otherLine--) = *(lineStarts--);
  3065.                     
  3066.                 for (i = nLines;i >= 0;i--)
  3067.                     *(otherLine--) = tempLineStarts[i];
  3068.                 
  3069.                 (**theTE32KHandle).nLines += deltaLines;
  3070.             }
  3071.         }
  3072.     }
  3073.     
  3074. EXITPOINT:
  3075.     *theNumAffected = nLines;
  3076.     *theDeltaLines = deltaLines;
  3077. }
  3078.  
  3079.  
  3080.  
  3081.  
  3082.  
  3083. static void updateLine(register long firstLine, TE32KHandle theTE32KHandle,int doFirst, LongRect *updateClipRect)
  3084. {
  3085. Rect            tempRect;
  3086. RgnHandle        updateRgn;
  3087. LongRect        updateRect;
  3088. LongPoint        selPt;
  3089. unsigned char    doWrap;
  3090. long            deltaLines,numAffected;
  3091. GrafPtr            oldPort;
  3092.  
  3093.  
  3094.     updateRect = (**theTE32KHandle).viewRect;
  3095.     updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3096.     updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight;
  3097.     
  3098.     if (updateClipRect)
  3099.     {
  3100.         if (updateRect.top < updateClipRect->top)
  3101.             updateRect.top = updateClipRect->top;
  3102.         if (updateRect.bottom > updateClipRect->bottom)
  3103.             updateRect.bottom = updateClipRect->bottom;
  3104.         if (updateRect.left < updateClipRect->left)
  3105.             updateRect.left = updateClipRect->left;
  3106.         if (updateRect.right > updateClipRect->right)
  3107.             updateRect.right = updateClipRect->right;
  3108.     }
  3109.     
  3110.     doWrap = FALSE;
  3111.     
  3112.     if (firstLine > 0L && LineEndIndex(firstLine - 1L,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine])
  3113.     {
  3114.         doWrap = TRUE;
  3115.         firstLine--;
  3116.     }
  3117.     
  3118.     else if (LineEndIndex(firstLine,theTE32KHandle) != (**theTE32KHandle).lineStarts[firstLine + 1L])
  3119.         doWrap = TRUE;
  3120.     
  3121.     
  3122.     if (!doWrap && doFirst)
  3123.         TE32KUpdate(&updateRect,theTE32KHandle);
  3124.     
  3125.     else if (doWrap)
  3126.     {
  3127.         CalParagraph(firstLine,theTE32KHandle,&deltaLines,&numAffected);
  3128.         
  3129.         if (deltaLines == 0L)
  3130.         {
  3131.             updateRect = (**theTE32KHandle).viewRect;
  3132.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3133.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  3134.         }
  3135.         
  3136.         else if (deltaLines > 0L)
  3137.         {
  3138.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  3139.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine + numAffected - deltaLines) * (**theTE32KHandle).lineHeight;
  3140.             
  3141.             GetPort(&oldPort);
  3142.             SetPort((**theTE32KHandle).inPort);
  3143.     
  3144.             updateRgn = NewRgn();
  3145.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  3146.             DisposeRgn(updateRgn);
  3147.             
  3148.             SetPort(oldPort);
  3149.             
  3150.             updateRect = (**theTE32KHandle).viewRect;
  3151.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3152.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  3153.         }
  3154.         
  3155.         else
  3156.         {
  3157.             LongRectToRect(&((**theTE32KHandle).viewRect),&tempRect);
  3158.             tempRect.top = (**theTE32KHandle).destRect.top + (firstLine - 1L + numAffected) * (**theTE32KHandle).lineHeight;
  3159.             
  3160.             GetPort(&oldPort);
  3161.             SetPort((**theTE32KHandle).inPort);
  3162.     
  3163.             updateRgn = NewRgn();
  3164.             ScrollRect(&tempRect,0,(**theTE32KHandle).lineHeight * deltaLines,updateRgn);
  3165.             
  3166.             SetPort(oldPort);
  3167.             
  3168.             updateRect.left = (**updateRgn).rgnBBox.left;
  3169.             updateRect.top = (**updateRgn).rgnBBox.top;
  3170.             updateRect.right = (**updateRgn).rgnBBox.right;
  3171.             updateRect.bottom = (**updateRgn).rgnBBox.bottom;
  3172.             
  3173.             DisposeRgn(updateRgn);
  3174.             
  3175.             TE32KUpdate(&updateRect,theTE32KHandle);
  3176.             
  3177.             updateRect = (**theTE32KHandle).viewRect;
  3178.             updateRect.top = (**theTE32KHandle).destRect.top + firstLine * (**theTE32KHandle).lineHeight;
  3179.             updateRect.bottom = updateRect.top + (**theTE32KHandle).lineHeight * numAffected;
  3180.         }
  3181.         
  3182.         TE32KUpdate(&updateRect,theTE32KHandle);
  3183.     }
  3184.  
  3185.     TE32KGetPoint((**theTE32KHandle).selStart,&selPt,theTE32KHandle);
  3186.     (**theTE32KHandle).selPoint = selPt;
  3187. }
  3188.  
  3189.  
  3190.  
  3191.  
  3192.  
  3193.  
  3194. static int    shiftKeyDown()
  3195. {
  3196. KeyMap    theKeyMap;
  3197.  
  3198.     GetKeys(theKeyMap);
  3199.     
  3200.     if (theKeyMap[1] & 0x01)
  3201.         return(TRUE);
  3202.     else
  3203.         return(FALSE);
  3204. }
  3205.  
  3206.  
  3207.  
  3208.  
  3209. static void MyClicker(void)
  3210. {
  3211. int            lineHeight;
  3212. Rect        viewRect;
  3213. Point        mousePoint;
  3214. RgnHandle    saveClip;
  3215. long        hDelta,vDelta;
  3216.  
  3217.     if (clickedTE32KH)
  3218.     {
  3219.         LongRectToRect(&((**clickedTE32KH).viewRect),&viewRect);
  3220.         lineHeight = (**clickedTE32KH).lineHeight;
  3221.     
  3222.         hDelta = 0L;
  3223.         vDelta = 0L;
  3224.         
  3225.         GetMouse(&mousePoint);
  3226.         
  3227.         if (!PtInRect(mousePoint,&viewRect))
  3228.         {
  3229.             if (mousePoint.v > viewRect.bottom && (**clickedTE32KH).viewRect.bottom < (**clickedTE32KH).destRect.top + (long) lineHeight * (**clickedTE32KH).nLines)
  3230.                 vDelta = -lineHeight;
  3231.             
  3232.             else if (mousePoint.v < viewRect.top && (**clickedTE32KH).viewRect.top > (**clickedTE32KH).destRect.top)
  3233.                 vDelta = lineHeight;
  3234.             
  3235.             
  3236.             if (mousePoint.h > viewRect.right && (**clickedTE32KH).viewRect.right < (**clickedTE32KH).destRect.right)
  3237.                 hDelta = -lineHeight;
  3238.             
  3239.             else if (mousePoint.h<viewRect.left && (**clickedTE32KH).viewRect.left > (**clickedTE32KH).destRect.left)
  3240.                 hDelta = lineHeight;
  3241.         }
  3242.         
  3243.         if (hDelta || vDelta)
  3244.         {
  3245.             saveClip = NewRgn();
  3246.             GetClip(saveClip);
  3247.             viewRect = (*((**clickedTE32KH).inPort)).portRect;
  3248.             ClipRect(&viewRect);
  3249.             
  3250.             TE32KScroll(hDelta,vDelta,clickedTE32KH);
  3251.             
  3252.             SetClip(saveClip);
  3253.             DisposeRgn(saveClip);
  3254.         }
  3255.     }
  3256. }
  3257.  
  3258.  
  3259.  
  3260.  
  3261. static void MyClickLoop(void)
  3262. {
  3263.     asm
  3264.     {
  3265.         movem.l        d1-d7/a0-a6,-(sp)
  3266.         jsr            MyClicker
  3267.         movem.l        (sp)+,d1-d7/a0-a6
  3268.         moveq.l        #1,d0
  3269.         rts
  3270.     }
  3271. }
  3272.  
  3273.  
  3274.  
  3275.  
  3276. void    TE32KAutoView(char autoView, TE32KHandle theTE32KHandle)
  3277. {
  3278.     if (theTE32KHandle)
  3279.     {
  3280.         if (!autoView)
  3281.             (**theTE32KHandle).clikLoop = nil;
  3282.         else
  3283.             (**theTE32KHandle).clikLoop = (TE32KProcPtr) MyClickLoop;
  3284.     }
  3285. }
  3286.  
  3287.  
  3288.  
  3289.  
  3290.